【发布时间】:2014-05-27 01:32:07
【问题描述】:
我在 Django 1.6.2 上收到 "TemplateDoesNotExist at /" 错误。 Stackoverflow 多次介绍过,但到目前为止没有任何效果。
模板是它们自己文件夹的一部分,不存储在应用程序中。
manage.py
project_dir
|_ settings.py
|_ urls.py
|_ apps
\_ app1
\_ views.py
|_ urls.py
|_ models.py
|_ templates
\_ base.html
\_ pages
\_ index.html
我已将TEMPLATE_DIR 和TEMPLATE_LOADERS 设置如下:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(os.path.join(BASE_DIR, 'project/apps/'))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'project/templates/'),
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
当我打印文件夹TEMPLATE_DIRS 和BASE_DIR 时,它们完全没问题。
当我复制TEMPLATE_DIRS的打印输出,直接在浏览器中查看,就可以查看模板了。
我注意到一个奇怪的事情是加载程序django.template.loaders.filesystem.Loader 没有在错误消息中列出任何文件夹。 django.template.loaders.app_directories.Loader 列出了一些与应用程序相关的文件夹。
模板通过urls.py调用
from django.views.generic.base import TemplateView
...
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='app1/index.html'), name='index'),
...
)
模板报错的原因是什么?
【问题讨论】:
标签: python django templates django-templates