0

This question is the sequel of my previous question - Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name (solved)

I have moved from one error (in the title of previous question) to another, (in the title of this question) but hopefully this one will not be that much difficult to solve.

I have this anchor tag on my homepage -

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

I have this view in my views.py file -

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

And 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'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
Nimantha
  • 6,405
  • 6
  • 28
  • 69
JanKoci
  • 59
  • 11
  • 1
    You can try a few of the solutions found here: https://stackoverflow.com/questions/1926049/django-templatedoesnotexist if you haven't already. Just double check that everything is in the right place. – Shyrtle Jan 29 '21 at 17:13

2 Answers2

1

these will solve it for sure.

1.if your saved your templates in a separate templates by app name in templates directory you have to put the name of the sub-folder before the name of the template like this hobbies/hobbieswithCSS.html . 2. set the root of the template directory in your settings.py for project and templates.

BASE_DIR = Path(__file__).resolve().parent.parent

TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)

also check this out Django TemplateDoesNotExist?

  • I am sorry, but I don´t understand You. I can show You my BASE_DIR and TEMPLATE_DIR, but I am kind of exhausted after dealing with these errors. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) and TEMPLATE_DIR = os.path.join(BASE_DIR,'templates') – JanKoci Jan 29 '21 at 18:07
  • My templates folder has one subfolder called basic_app and there are all of my templates. – JanKoci Jan 29 '21 at 18:08
  • then your template path in view should be like return render(request,'basic_app/hobbieswithCSS.html') – Siva Sankar Jan 29 '21 at 18:44
  • the problem persists after doing so – JanKoci Jan 29 '21 at 19:04
0

Change your views.py like this:

def hobbieswithCSS(request):
    return render(request,'basic_app/hobbieswithCSS.html')
Siva Sankar
  • 1,672
  • 1
  • 9
  • 16