django.template.exceptions.TemplateDoesNotExist:/hello/

时间:2020-12-23 13:54:21

标签: python django

我创建了一个函数 hello>view.py

def index (request):
    return render(request,"hello/index.html")

和 hello>template>hello>index.html 中的 HTML 文件, 但这是我所拥有的

Internal Server Error: /hello/
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/douju/lecture3/hello/views.py", line 7, in index
    return render(request,"hello/index.html")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: hello/index.html

这就是我的 TEMPLETS 中的内容:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

2 个答案:

答案 0 :(得分:0)

我相信您在需要模板时已经创建了一个模板文件夹(注意末尾的 S)。

答案 1 :(得分:0)

Django 应该自动搜索位于每个 int (*)(const char *, const char *)INSTALLED_APP 目录中的模板。如果 Django 没有找到模板,则您的 templates/ 中可能包含 TEMPLATES['APP_DIRS']=False。如果它仍然找不到您的模板文件,请考虑使用 settings.py 设置指定其他模板目录:

TEMPLATES['DIRS']

Django 模板设置:https://docs.djangoproject.com/en/3.1/ref/settings/#templates