我收到以下消息。我有点理解我的项目服务器找不到模板。但是,我不知道它到底对模板有什么抱怨。听到有关发生的情况的替代解释将非常好。
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 1.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/stanley/mytemplates/polls/detail.html, polls/poll_detail.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/polls/detail.html, polls/poll_detail.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/polls/detail.html, polls/poll_detail.html (File does not exist)
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
136. response = response.render()
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
104. self._set_content(self.rendered_content)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
79. template = self.resolve_template(self.template_name)
File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in resolve_template
55. return loader.select_template(template)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in select_template
193. raise TemplateDoesNotExist(', '.join(not_found))
Exception Type: TemplateDoesNotExist at /polls/1/
Exception Value: polls/detail.html, polls/poll_detail.html
我将尝试更深入地回答这个问题,特别是对您的声明“我认为我把模板放在错误的位置”做出反应。使用您发布的追溯可以轻松解决此问题。
它首先告诉您有关您的环境的一些信息-您可以简单地忽略它。这对于TemplateLoaderError没有用。但是,如果您的任何应用程序都需要特殊版本的Python或类似的东西,这可能很重要。
接下来要发现的是发生了什么样的错误,这对于您的特殊情况非常有帮助:
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/stanley/mytemplates/polls/detail.html, polls/poll_detail.html (File does not exist)
如果您仔细看一下,它会告诉您Django试图在哪里找到指定的模板,以及在寻找模板时发生了什么(文件不存在)。这将帮助您找到一种解决方案来检查您的模板是否确实放置在错误的位置(这种情况经常发生,即使是经验丰富的Django开发人员也是如此)。
下一部分是回溯。在这种情况下,您可以轻松地忽略它,因为项目文件夹中没有涉及的文件。如果是的话,那意味着您可能会在命名文件中弄乱某些东西,而Django甚至会告诉您代码(可能)在哪一行中引发了异常。
最后一部分是有关已引发的异常的信息,在您的情况下为TemplateDoesNotExist。 Django有一些特殊的例外,您可以找到here。除此之外,Django提出了标准的Python异常。
希望我没有告诉你太多你已经知道的东西。
您的设置中是否有类似内容?
PROJECT_ROOT = path.dirname(path.abspath(__file__)) #gets directory settings is in
TEMPLATE_DIRS = (
PROJECT_ROOT+'/templates',
)
[您可能已经在settings.py文件的TEMPLATE_DIRS元组中指定了一些路径。Django仅在这些目录中搜索html模板。因此将以下路径添加到TEMPLATE_DIRS。
TEMPLATE_DIRS = ('/polls',)