Django 1.8忽略了DIRS,找不到模板

时间:2015-09-30 08:59:48

标签: python django templates

以下是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错误。这是Template-loader postmortem,它不包括我的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。这可能是个问题。

2 个答案:

答案 0 :(得分:2)

日志条目后

  

使用loader django.template.loaders.filesystem.Loader:

加载程序应该打印它试图找到模板的所有可能位置,就像在app_directories加载器的类似日志之后一样。如果此处没有路径,请仔细检查您的TEMPLATES设置是否正确定义,以后是否在设置或其他文件中的某处覆盖,以及Django是否正确加载该设置文件。

DEBUG设置为True时,您可以抛出一些异常来获取500服务器错误,在错误页面上,您将获得Django看到的所有设置的方便列表。

另外检查您的WSGI服务器是否正在加载正确版本的django,旧版本没有TEMPLATE设置(它在多个设置之间分割)。

答案 1 :(得分:0)

问题与错误的virtualenv和实际使用Django 1.5.1而不是1.8有关。我将保存这篇文章,也许这将有助于将来遇到类似问题的人。