Ошибка Django TemplateDoesNotExist - неверный путь к папке templates

В настоящее время я смотрю лекцию по Django из CS50W и кодировал вместе с Брайаном просто отлично. Но теперь я продолжаю получать одну и ту же ошибку, что бы я ни делал. Я хочу отобразить простую HTML5 страницу с помощью Django, и хотя я указал правильный путь к папке templates и index.html. Он переходит в папку hello/templates/ и пытается найти там newyear/index.html.

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
    })

И я получаю ошибку, как указано ниже

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


Структура каталогов проекта

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

В newyear ваш каталог называется /template, а не /templates - добавьте 's' к названию каталога и все должно работать.

Вернуться на верх