I am currently watching the Django lecture from CS50W and was coding along with Brian just fine. But now I keep getting the same error no matter what I do. I want to render the simple HTML5 page with Django and even though I have specified the correct path to the templates folder and index.html.
It goes to hello/templates/
and tries to find newyear/index.html
there.
from Django.shortcuts import render
from datetime import datetime
# Create your views here.
def index(request):
now = datetime.now()
return render(request, 'newyear/index.html', {
'newyear': now.month == 1 and now.day == 1
})
And I get an error as stated below
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.app_directories.Loader:
/home/gaba/cs50w/week3-Django/lecture3/hello/templates/newyear/index.html
(Source does not exist)
* django.template.loaders.app_directories.Loader:
/usr/local/lib/python3.10/dist-packages/django/contrib/admin/templates/newyear/index.html
(Source does not exist)
* django.template.loaders.app_directories.Loader:
/usr/local/lib/python3.10/dist-packages/django/contrib/auth/templates/newyear/index.html
(Source does not exist)
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/gaba/cs50w/week3-Django/lecture3/newyear/views.py", line 8, in index
return render(request, 'newyear/index.html', {
File "/usr/local/lib/python3.10/dist-packages/django/shortcuts.py", line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/usr/local/lib/python3.10/dist-packages/django/template/loader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "/usr/local/lib/python3.10/dist-packages/django/template/loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /newyear/
Exception Value: newyear/index.html
Directory structure of the project
Lecture 3/
├── lecture3/
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── hello/
│ ├── templates/
│ │ └── hello/
│ │ ├── greet.html
│ │ └── index.html
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── newyear/
│ ├── template/
│ │ └── newyear/
│ │ └── index.html
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
└── manage.py
CodePudding user response:
In newyear your directory is called /template not /templates - add an 's' to the directory name and it should work.