1

I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.

I have this anchor tag on my homepage -

<a href="{% url 'basic_app:hobbieswithCSS.html' %}">My Hobbies</a>

I have this view in my views.py file -

def hobbieswithCSS(request):
    return render(request,'basic_app/hobbieswithCSS.html')

I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^admin/', admin.site.urls),
    url(r'^basic_app/',include('basic_app.urls')),
    url(r'^logout/$',views.user_logout,name='logout'),
    url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]

Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
JanKoci
  • 59
  • 11
  • 1
    Did you set your template location up in your settings.py? – Shyrtle Jan 29 '21 at 15:18
  • 1
    I have this line of code in my settings.py file - TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') – JanKoci Jan 29 '21 at 15:20
  • 1
    And are other templates on your project working? – Shyrtle Jan 29 '21 at 15:22
  • 1
    yes everything else is working - login.html, registration.html and index.html, while all of these are inheriting from base.html – JanKoci Jan 29 '21 at 15:25
  • 1
    Maybe put up the rest of your views.py file. It looks like you are including 'basic_app' and redirecting those views to basic_app/... but then you are declaring views.hobbieswithCSS in the main URL and not in the basic_app. When you are calling your URL in the template, you are pointing it at basic_app/hobbieswithCSS. – Shyrtle Jan 29 '21 at 15:29
  • 2
    @JanKoci, it must be: My Hobbies. Without basic_app – NKSM Jan 29 '21 at 15:29
  • 1
    The code is the same as it was in the beginning and now I am getting different error, which is weird as well - (I don´t think the problem is in the rest of views.py, because I have just copied that code from one tutorial) django.core.exceptions.SuspiciousFileOperation: The joined path (P:\hobbieswithCSS.html) is located outside of the base path component (C:\Users\Javorina\Desktop\NEW CV\learning_users\static) – JanKoci Jan 29 '21 at 15:37
  • 1
    Share your basic_app urls. And don’t change your question. Do simple addition on the end of your question. – NKSM Jan 29 '21 at 15:39
  • 1
    I have just two lines of code in ulrs.py under basic_app - from django.conf.urls import url (+) from basic_app import views – JanKoci Jan 29 '21 at 15:41
  • 1
    is your index.html and your hobbieswithcss.html in the same place? – Shyrtle Jan 29 '21 at 15:42
  • that plus is not contained in the file, but I am not sure how to separate lines in these comments – JanKoci Jan 29 '21 at 15:42
  • yes it is both situated inside the basic_app folder, which is situated under templates folder – JanKoci Jan 29 '21 at 15:43
  • 1
    Try in your view: render(request,'hobbieswithCSS.html') – NKSM Jan 29 '21 at 15:44
  • Now I am really confused, because that anchor tag (i have removed the basic_app from it) is now redirecting me to index.html instead to hobbieswithCSS.html, I have never seen anything like this before :O – JanKoci Jan 29 '21 at 15:44
  • even after changing the view to render(request,'hobbieswithCSS.html') it´s still redirecting me to index.html :( – JanKoci Jan 29 '21 at 15:46
  • 1
    i don´t understand it, because the anchor tag says clearly - go to hobbieswithCSS.html - My Hobbies and when I try to add .html to that anchor tag, then I end up having the same problem, which I had at the very beginning – JanKoci Jan 29 '21 at 15:48
  • I just want to be able to use a new template. IDK why it has to be so complicated. – JanKoci Jan 29 '21 at 15:48
  • 1
    Try changing your view so instead of rendering we just give a direct HttpResponse. Make sure to include `from django.http import HttpResponse` then visit the page directly. – Shyrtle Jan 29 '21 at 15:50
  • You have the same path for index and hobbies . You have to change your path – NKSM Jan 29 '21 at 15:51
  • I have tried the HttpResponse and the problem still persists. :( It is still redirecting me to index.html, if I don´t have the .html suffix in the anchor tag or giving me the error, if I am including the .html suffix in the anchor tag. – JanKoci Jan 29 '21 at 15:56
  • @JanKoci, I have updated my answer, make these changes and it will be work. Your view is ok, you can use render. – NKSM Jan 29 '21 at 15:56
  • Well, it is interesting. I am not getting any error in my command line or in my browser, but the webpage is completely blank. I am not sure, if it is related to the previous problem or if the previous problem has been solved and this is a new one. My hobbieswithCSS.html looks like this - {% extends "basic_app/base.html" %} {% block body_block %} {% load staticfiles %} hml code (I am not showing the whole code, because this comment window doesn´t allow me to write so many characters) {% endblock %} – JanKoci Jan 29 '21 at 16:09
  • But I knew from the very beginning, that the core of the problem will be in the urlpatterns, because I don´t understand, how these symbols such as caret or dollar sign really work. – JanKoci Jan 29 '21 at 16:12
  • 1
    @JanKoci, this - {% extends "base.html" %}. Same remove basic_app. – NKSM Jan 29 '21 at 16:13
  • The page is still blank. I have removed that basic_app and I have tried both base.html and solely base, and the page is still white like pampers. :O – JanKoci Jan 29 '21 at 16:17
  • oh wait please, I might need to check it again – JanKoci Jan 29 '21 at 16:18
  • The page was blank, because I forgot to remove the HttpResponse, (it was probably because of it) so when I changed it to render, now I am getting an error, that TemplateDoesNotExist at /hobbies/ :O – JanKoci Jan 29 '21 at 16:26
  • Anyways thank You very much. I am glad, that I have moved from more complicated error to less complicated one. At least I hope so. I am really grateful for this assistance. – JanKoci Jan 29 '21 at 16:35
  • I have posted a new question, where I have listed the relevant lines of code with the important changes, that have been done during our brainstorming. https://stackoverflow.com/questions/65958807/templatedoesnotexist-at-hobbies – JanKoci Jan 29 '21 at 17:13

2 Answers2

3

It must be:

<a href="{% url 'hobbieswithCSS' %}">My Hobbies</a>

Without basic_app.

Also you have the same path for index and hobbies . You have to change your url path:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    ...
    url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
NKSM
  • 5,422
  • 4
  • 25
  • 38
0

in your template update to

<a href="{% url 'basic_app:hobbieswithCSS' %}">My Hobbies</a>
  • I am still getting almost the same error, when I do this - Reverse for 'hobbieswithCSS' not found. 'hobbieswithCSS' is not a valid view function or pattern name. – JanKoci Jan 29 '21 at 14:23