Django 1.8忽略DIRS,找不到模板 [英] Django 1.8 ignores DIRS, cannot find templates

查看:59
本文介绍了Django 1.8忽略DIRS,找不到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在settings.py中的模板"部分:

  TEMPLATES = [{'BACKEND':'django.template.backends.django.DjangoTemplates',"DIRS":[os.path.join(BASE_DIR,'模板'),],'APP_DIRS':是的,'选项': {'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验尸报告,其中不包括我的DIRS目录:

  Django尝试按以下顺序加载这些模板:使用loader django.template.loaders.filesystem.Loader:使用loader django.template.loaders.app_directories.Loader:/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/layouts/default.html(文件不存在)/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/layouts/default.html(文件不存在)/home/foundation/public/foundation/foundation/event_request/templates/layouts/default.html(文件不存在) 

**更新:**似乎apache加载django 1.5而不是django 1.8.这可能是个问题.

解决方案

登录后

使用加载程序django.template.loaders.filesystem.Loader:

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

DEBUG 设置为 True 时,您可能会引发一些异常,从而导致500个服务器错误,在错误页面上,您将获得Django看到的所有设置的便捷列表./p>

还要检查您的WSGI服务器是否正在加载django的正确版本,较旧的版本没有 TEMPLATE 设置(它在多个设置之间分配).

Here is my TEMPLATES section in settings.py:

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',
            ],
        },
    },
]

I'm using template inside application directory and it works. But whenever I'm trying to extend it with layout from global templates directory (os.path.join(BASE_DIR, 'templates'), which exists, I tested), django raises TemplateDoesNotExist error. Here is Template-loader postmortem, which does not includes my DIRS directory:

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)

**UPDATE: ** seems like apache loads django 1.5 instead of django 1.8. This might be a problem.

解决方案

After log entry

Using loader django.template.loaders.filesystem.Loader:

loader should print all possible locations that it tried to find your template, like it was after similar log from app_directories loader. If there is no paths here, double check if your TEMPLATES setting is defined correctly, is not overwritten somewhere later in your settings or other files and if Django is loading that settings file correctly.

You can throw some exception to get 500 server error when DEBUG is set to True, on error page you will get handy list of all settings that Django sees.

Also check if your WSGI server is loading proper version of django, older versions doesn't have TEMPLATE setting (it was splitted between multiple settings).

这篇关于Django 1.8忽略DIRS,找不到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆