Django:基本模板的TemplateDoesNotExist错误

sac*_*ure 1 django templates template-inheritance

我了解了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',
        ],
    },
},
]
Run Code Online (Sandbox Code Playgroud)

如果未扩展,则所有模板均正确渲染;如果是父模板,则所有模板均正确呈现。

我该怎么办?

Sak*_*sow 5

您正在使用Django 扩展方式错误,应该使用父模板名称,因此请执行以下操作:

{% extends "base.html" %}

编辑

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

假设您以这样的方式渲染“ templates / child_page.html”,那么您应该以相同的方式进行扩展 {% extends "templates/base.html" %}