【发布时间】:2017-05-28 06:33:18
【问题描述】:
我有一个 base.html 模板,我网站上的每个页面都将扩展它。但是,根据我收集到的信息,Django 要求模板应该在 app 目录中。从应用程序中,如何扩展我位于项目的 basedirectory/templates/ 中的模板?
这是我的目录的排序方式:
project/
|----templates/
| |----base.html
|
|----app1/
| |----templates/
| |----app1/
| |----base.html
| |----index.html
|
|----app2/...
目前,我有 index.html 扩展 app1/base.html:
{% extends 'welcome/base.html' %}
但是,这并不理想,因为我想扩展 project/templates/base.html。从逻辑上讲,我的 app1 中不应该有模板。
Settings.py:
TEMPLATE_DIRS = (
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, 'templates').replace('\\', '/'),
)
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',
],
},
},
]
错误是当我的 app1/views.py 尝试渲染时:
{% extends 'welcome/base.html' %}
【问题讨论】:
标签: python django django-templates