【问题标题】:Django TemplateDoesNotExist Error on Windows machineWindows机器上的Django TemplateDoesNotExist错误
【发布时间】:2015-06-15 11:51:56
【问题描述】:

我一直在关注 Django 的教程 Tango with Django

我试图按照link 上的说明添加模板。 我正在 Windows 7 机器上使用 Python 2.7、Django 1.8。

以下是我得到的错误:

TemplateDoesNotExist at /rango/
rango/index.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/rango/
Django Version: 1.8
Exception Type: TemplateDoesNotExist
Exception Value:rango/index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in get_template, line 46

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\rango\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\rango\index.html    (File does not exist)

下面是我的文件结构:

tango_with_django_project
+-- rango
|   +--views.py
|   +--other files
+-- tango_with_django_project
|   +--templates
|   |   +--rango
|   |   |  +--index.html
|   +--settings.py
|   +--other files
+-- db.sqlite3
+-- manage.py`

我在settings.py中给出了如下模板路径:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR,'templates')
TEMPLATE_DIRS = (
    TEMPLATE_PATH,
)

并将视图设置为 views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
   context_dict = {'boldmessage': "I am bold font from the context"}
   return render(request, 'rango/index.html', context_dict)

不使用模板,它可以正常工作并显示纯文本。但是当我对模板进行所需的更改时,它不起作用并引发错误。 SO上有很多链接都有同样的问题,但没有一个对我有用。 one 具有几乎相同的错误描述并且在 Windows 机器上有一个模糊的答案。

我尝试直接指定绝对路径,而不是从os.path 获取它,并且还尝试将模板文件夹放在不同的路径中。

任何帮助将不胜感激。

【问题讨论】:

  • Django 没有查看您的TEMPLATE_DIRS。你确定你没有覆盖那个变量吗?在您看来,请尝试使用from django.conf import settings; print(settings.TEMPLATE_DIRS)
  • @AndreaCorbellini :即使我认为TEMPLATE_DIRS 没有设置为我指定的值。它检查这些路径中的模板C:\Python27\lib\site-packages\django\contrib\admin;C:\Python27\lib\site-packages\django\contrib\auth 我也尝试了你告诉我的内容并得到以下错误:django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DIRS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
  • 你得到的错误是不言自明的。你设置了DJANGO_SETTINGS_MODULE 还是打电话给settings.configure()?您的 Web 应用程序是如何运行的? (manage.py,一个网络服务器,...)
  • 它在开发服务器上运行,使用命令python manage.py runserver

标签: python django


【解决方案1】:

对我有用的解决方案是从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',
            ],
        },
    },
]

并添加: TEMPLATE_DIRS = ('C:/Users/vaulstein/tango_with_django_project/templates',)

更改下面的第 4 行:

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

【讨论】:

    【解决方案2】:

    您可以简单地将应用程序名称添加到 settings.py

    中的 INSTALLED_APPS 列表中

    在settings.py中找到以下列表,

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles'
    ]
    

    将您的应用程序名称添加到该列表中。在你的情况下,

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'tango_with_django_project'
    ]
    

    【讨论】:

      【解决方案3】:

      在我的情况下,修改 INSTALLED_APPS 没有帮助,所以根据之前的答案,我只是通过以下方式修改了我的 settings.py

      1. 添加import OS
      2. os.getcwd() 添加到DIRS

      所以它看起来像这样:

      ...
      from pathlib import Path
      import os
      ...
      TEMPLATES = [
          {
              'BACKEND': 'django.template.backends.django.DjangoTemplates',
              'DIRS': [os.getcwd()],
              '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',
                  ],
              },
          },
      ]
      

      【讨论】:

        猜你喜欢
        • 2011-07-18
        • 2014-02-19
        • 2013-06-20
        • 2015-09-02
        • 2012-12-18
        • 1970-01-01
        • 2014-05-27
        • 2021-12-28
        • 2018-09-10
        相关资源
        最近更新 更多