【发布时间】:2015-12-27 23:55:01
【问题描述】:
这是我在 settings.py 中的 TEMPLATES 部分:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
我在应用程序目录中使用模板,它可以工作。但是,每当我尝试使用全局模板目录(os.path.join(BASE_DIR, 'templates'),它存在,我测试过)的布局来扩展它时,django 都会引发 TemplateDoesNotExist 错误。这是模板加载器事后分析,其中不包括我的 DIRS 目录:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/layouts/default.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/layouts/default.html (File does not exist)
/home/foundation/public/foundation/foundation/event_request/templates/layouts/default.html (File does not exist)
**更新:** 似乎 apache 加载 django 1.5 而不是 django 1.8。这可能是个问题。
【问题讨论】:
-
你是如何定义 BASE_DIR 的?
-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
-
是否需要是完整的目录路径而不是路径片段?
-
完整路径。这里的 TEMPLATE 结构看起来像运行时: TEMPLATES [{'APP_DIRS': True, 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['/home/foundation/public/foundation/templates' ],[...]
-
对我来说更有趣的是目录没有在 Template-loader postmortem 中列出。