我有一个具有以下结构的项目:
project
project
static/
templates
project
base.html
__init__.py
.....
events
static/
templates
events
events.html
__init__.py
models.py
.....
问题
我想在 events.html
中扩展 base.html
我试过:
{% extends "project:project/base.html" %}
但得到以下错误:
Exception Type: TemplateDoesNotExist
Exception Value: project:project/base.html
也试过:
{% extends "base.html" %}
但返回相同的错误:
Exception Type: TemplateDoesNotExist
Exception Value: base.html
在 INSTALLED_APPS
我有:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'events',
'project',
)
TEMPLATE_DIRS
和 BASE_DIR
:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
在 TEMPLATE_LOADERS
我有:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
最佳答案
您的 TEMPLATE_DIR
设置为 project/templates
,而不是 project/templates/project
。
由于project/templates
中没有base.html
,Django返回错误。
您可以将 base.html
移至 project/templates
,或通过以下方式引用它:
{% extends "project/base.html" %}
关于python - 如何从另一个模块扩展模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701479/