0

我正在关注民意调查应用教程第 3 部分,但找不到模板

这是确切的错误

polls/index.html
Request Method: GET
Request URL:    http://localhost:8000/polls/
Django Version: 1.4.3
Exception Type: TemplateDoesNotExist
Exception Value:    
polls/index.html
Exception Location: c:\Python27\lib\site-packages\django\template\loader.py in find_template, line 138
Python Executable:  c:\Python27\python.exe
Python Version: 2.7.2

所以在我的 settings.py 中,我把目录放在那里。“C:/Scripts/mysite/template”并创建/polls/index.html

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "C:/Scripts/template"
    "C:/Scripts/mysite/template"
)

它由于某种原因找不到它。有任何想法吗?这是教程中的确切说明。

在文件系统的某个位置创建一个目录,Django 可以访问其内容。(Django 以您的服务器运行的任何用户身份运行。)

4

2 回答 2

2

您在第一行末尾缺少一个逗号:

"C:/Scripts/template",   # <--- HERE
"C:/Scripts/mysite/template"

没有它,Python 会自动连接两个字符串(因为它们在括号内),这样它就变成了"C:/Scripts/templateC:/Scripts/mysite/template".

于 2013-02-02T09:15:57.753 回答
1

调试

  1. 从您的应用程序主文件夹中,运行以下命令:python manage.py shell这将使用设置文件引导您的应用程序。
  2. 输入from django.conf import settings并按回车
  3. 输入settings.TEMPLATE_DIRS并检查输出。您看到您指定的模板目录了吗?

相对于 settings.py 文件的绝对路径

我通常使用相对于 settings.py 文件的绝对路径。这样,协作者可以共享一个主设置文件,并且无论您将什么系统/环境部署到您的路径都是正确的。

去做这个:

# settings.py
import os

# ... other settings

TEMPLATE_DIRS = (
    os.path.join(os.path.normpath(os.path.dirname(__file__)), 'templates'),
)

如果调试步骤没有帮助,请告诉我,我会尝试提供更多帮助。

于 2013-02-02T00:49:37.837 回答