【问题标题】:Django 1.8 TEMPLATE-DEBUG = True setting sets off Template Not Found ErrorDjango 1.8 TEMPLATE-DEBUG = True 设置会引发 Template Not Found 错误
【发布时间】:2015-08-10 09:52:02
【问题描述】:

DJANGO 1.8 Python 3.4

我之前遇到过 TEMPLATES 数组设置生成错误“找不到模板”,而 Django 模板加载器实际上报告它找到了模板。我可以通过恢复到 TEMPLATE_DIRS 设置来解决这个问题。See the Question

我需要调试一个模板,所以我将 TEMPLATE_DEBUG 设置为 True...,它再次生成 Template not found 错误:

index.html

请求方法:GET 请求网址:http://127.0.0.1:7000/ Django 版本:1.8.2 异常类型:TemplateDoesNotExist 异常值:

index.html

Exception Location:     /home/sdr/sl/lib/python3.4/site-packages/django/template/loader.py in render_to_string, line 138
Python Executable:  /home/sdr/sl/bin/python
Python Version:     3.4.3
Python Path:    

['/home/sdr/sl/agryp',
 '/home/sdr/pycharm-4.5/helpers/pydev',
 '/home/sdr/sl/src/tastypie',
 '/home/sdr/sl/agryp',
 '/usr/local/lib/python34.zip',
 '/usr/local/lib/python3.4',
 '/usr/local/lib/python3.4/plat-linux',
 '/usr/local/lib/python3.4/lib-dynload',
 '/home/sdr/sl/lib/python3.4/site-packages']

Server time:    Thu, 28 May 2015 04:05:11 +0000
Template-loader postmortem

Django tried loading these templates, in this order:

    Using loader django.template.loaders.filesystem.Loader:
        /home/sdr/sl/agryp/templates/index.html (File exists)

无论我使用 TEMPLATES=[] 设置还是 TEMPLATE_DIRS 设置,错误仍然存​​在。

我希望能深入了解这个问题。

【问题讨论】:

    标签: django debugging templates


    【解决方案1】:

    确保您在 settings.py 中提供了模板文件夹路径或检查您的模板路径在 views.py 中是否正确

    在 Django 1.8 中会是这样的

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [os.path.join(BASE_DIR, 'templates')],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    

    这对我有用。

    【讨论】:

    • 感谢您的回复。是的,确实,路径和模板设置是正确的 - 因为加载程序显示找到的文件。它甚至适用于我的新项目。在我将代码添加到我的项目之后,TEMPLATES= 设置。我暂时决定继续编码。这是我添加的应用程序之间的一些模糊交互(如美味派等),无疑需要时间来追踪。
    猜你喜欢
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2013-07-16
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多