【发布时间】:2020-04-15 09:56:57
【问题描述】:
我在设置模板(在 app 目录下)以扩展 root/templates/jinja2 目录中的 base.html 时遇到问题
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
os.path.join(BASE_DIR, 'templates/jinja2')
],
'APP_DIRS': True,
...
},
}
]
文件夹结构:
apps
|---app1
|---templates
|---jinja2
|---listing.html
|---templates
|---jinja2
|---base.html
listing.html
{% extends "base.html" %}
...
我拉入listing.html时遇到的错误:
TemplateDoesNotExist at ...
base.html
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /webapps/pickup/env/lib/python3.7/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /webapps/pickup/env/lib/python3.7/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /webapps/pickup/src/apps/listing/templates/base.html (Source does not exist)
在根模板文件夹中扩展基本模板的正确方法是什么?我正在使用 Django 3.0.5
【问题讨论】:
-
只是要注意,如果我把listing.html放在root/templates/jinja2中,没有错误。
-
找出问题所在:
listing.html应该在app1/jinja2/中,而不是在app1/templates/jinja2/中