【发布时间】: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