【问题标题】:TemplateDoesNotExist but it existTemplateDoesNotExist 但它存在
【发布时间】:2017-02-01 13:40:43
【问题描述】:

Python 2.7 和 Django 1.10 我的模板存在,但我做错了!

TemplateDoesNotExist at /basicview/2/
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>TEST</title>
</head>
<body>
This is template_two view!
</body>
</html>

Request Method:     GET
Request URL:    http://127.0.0.1:8000/basicview/2/
Django Version:     1.10.1
Exception Type:     TemplateDoesNotExist
Exception Value:    

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>TEST</title>
</head>
<body>
This is template_two view!
</body>
</html>

Exception Location:     /home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg/django/template/loader.py in get_template, line 25
Python Executable:  /home/i/djangoenv/bin/python
Python Version:     2.7.11
Python Path:    

['/home/i/djangoenv/bin/firstapp',
 '/home/i/djangoenv/lib/python2.7',
 '/home/i/djangoenv/lib/python2.7/plat-i386-linux-gnu',
 '/home/i/djangoenv/lib/python2.7/lib-tk',
 '/home/i/djangoenv/lib/python2.7/lib-old',
 '/home/i/djangoenv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/i/djangoenv/local/lib/python2.7/site-packages',
 '/home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg',
 '/home/i/djangoenv/lib/python2.7/site-packages',
 '/home/i/djangoenv/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg']

Server time:    Пт, 23 Сен 2016 15:43:30 +0000

settings.py(os.path.join(BASE_DIR)、'templates' 或 /home/mainapp/templates)不起作用..

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['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',
            ],
        },
    },

article/views.py 我的 def 看起来像:

def template_two(request):
    view = "template_two"
    t = get_template('myview.html')
    html = t.render(Context({'name': view}))
    return render(request, html, {})

我的文件:

mainapp/mainapp/settings.py
mainapp/mainapp/article/views.py
mainapp/templates/myview.html

【问题讨论】:

    标签: python django python-2.7 django-templates


    【解决方案1】:

    我建议您将模板放入您的应用中。

    您的文件将在此处:

    mainapp/mainapp/templates/myview.html
    

    请确保将mainapp 添加到您的INSTALLED_APPS,如下所示:

    INSTALLED_APPS = [
       ...
       'mainapp',
    ]
    

    【讨论】:

    • 不,我的模板在 mainapp 中而不是在 mainapp/mainapp 中
    【解决方案2】:

    在您的 settings.py 中,您有 'DIRS': ['templates'],

    模板的路径是mainapp/templetes/myview.html

    你有错字templetes != templates。将带有模板的文件夹重命名为templates

    【讨论】:

    • 同样的错误:TemplateDoesNotExist at /basicview/2/myview.html
    【解决方案3】:

    问题是您手动渲染模板并同时使用render 快捷方式。你的get_template 工作正常,但是当你调用render(request, html, {}) 时,Django 将html 视为文件名,并寻找一个名为&lt;!DOCTYPE html&gt;\n&lt;html&gt;... 的模板文件。

    您应该手动渲染模板:

    def template_two(request):
        view = "template_two"
        t = get_template('myview.html')
        html = t.render({'name': view})  # Note you should use a plain dictionary, not `Context` on Django 1.8+
        return HttpResponse(html)
    

    或者,使用render 快捷方式更简单。

    def template_two(request):
        view = "template_two"
        return render(request, "myview.html", {'name': view})
    

    您还应该将DIRS 设置改回使用os.path.join(BASE_DIR, 'templates')。使用字符串'templates' 是行不通的。

    【讨论】:

      【解决方案4】:

      第 1 步:复制模板的路径。

      第 2 步:在您的设置中 DIRS 中过去的复制路径。py >> “模板”

      它对我有用

      【讨论】:

        【解决方案5】:

        在 Django 3.1.7 中,我刚刚重新启动了服务器“python manage.py runserver”,它工作正常。 我在 settings.py 中的条目是

                TEMPLATES = [
                    {
                        'BACKEND': 'django.template.backends.django.DjangoTemplates',
                        'DIRS': [],
                        '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',
                            ],
                        },
                    },
                ]
        

        【讨论】:

          【解决方案6】:

          截至 2021 年 5 月 5 日,我为解决此错误所做的一切都在 settings.py 中添加了“DIRS”的完整路径

          示例:

          settings.py

          TEMPLATES = [
              {
                  'BACKEND': 'django.template.backends.django.DjangoTemplates',
                  'DIRS': [r'the path to the templates folder here'],
                  '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',
                      ],
                  },
              },
          ]
          

          【讨论】:

            猜你喜欢
            • 2021-04-30
            • 2015-09-06
            • 2020-07-14
            • 2015-07-15
            • 2015-11-21
            • 2018-08-11
            • 1970-01-01
            • 2014-07-04
            • 1970-01-01
            相关资源
            最近更新 更多