Django:基本模板的 TemplateDoesNotExist 错误

Django: TemplateDoesNotExist error for base template

我了解了 Django 模板继承并正在研究它。

我在与其他模板相同的目录中制作了一个 base_post_login.html 模板。

并键入 {% extends "base_post_login.html" %} 作为子模板中的第一行。

但是当通过后端呈现子模板时,会引发 TemplateDoesNotExist 错误。

这是settings.py(相关部分):

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',
        ],
    },
},
]

如果不扩展父模板,所有模板都会正确呈现。

我该怎么办?

你使用 Django extends 的方式不对,它应该采用父模板名称,所以这样做:

{% extends "base.html" %}

编辑

好的,我明白了,您应该像渲染其他模板时一样使用模板路径:

假设你像这样渲染 "templates/child_page.html" 那么你应该以同样的方式扩展 {% extends "templates/base.html" %}