python - 需要有关 Django 教程的帮助

标签 python django

我在这里做 Django 教程:http://docs.djangoproject.com/en/1.2/intro/tutorial03/

我在 settings.py 中的 TEMPLATE_DIRS 如下所示:

TEMPLATE_DIRS = (

    "/webapp2/templates/"
    "/webapp2/templates/polls"
    # 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.

)

我的 urls.py 看起来像这样:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'^polls/$', 'polls.views.index'),
    (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
    (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
    (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
    (r'^admin/', include(admin.site.urls)),
)

我的 views.py 看起来像这样:

from django.template import Context, loader
from polls.models import Poll
from django.http import HttpResponse

def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    t = loader.get_template('c:/webapp2/templates/polls/index.html')
    c = Context({
        'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(t.render(c))

我认为我的模板路径错误,因为当我将 views.py 代码简化为类似这样的代码时,我能够加载页面。

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the poll index.")

我的索引模板文件位于 C:/webapp2/templates/polls/index.html。我究竟做错了什么?

This is the error I am getting:
Traceback (most recent call last):

  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)

  File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 141, in get_response
    return self.handle_uncaught_exception(request, resolver, sys.exc_info())

  File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 180, in handle_uncaught_exception
    return callback(request, **param_dict)

  File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 76, in _wrapped_view
    response = view_func(request, *args, **kwargs)

  File "C:\Python27\lib\site-packages\django\views\defaults.py", line 30, in server_error
    t = loader.get_template(template_name) # You need to create a 500.html template.

  File "C:\Python27\lib\site-packages\django\template\loader.py", line 157, in get_template
    template, origin = find_template(template_name)

  File "C:\Python27\lib\site-packages\django\template\loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)

TemplateDoesNotExist: 500.html

最佳答案

TEMPLATE_DIRS = (
    "/webapp2/templates/"
    "/webapp2/templates/polls"
)

如果您不在中间放置逗号,Python 会在编译期间连接这些字符串。

还有一件事:通常你不需要硬编码模板目录。对于大多数情况,以下内容应该足够了:

def fromRelativePath(*relativeComponents):
    return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")

TEMPLATE_LOADERS = (
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
)

TEMPLATE_DIRS = (
    fromRelativePath("templates"),
)

这将使 Django 在“yoursite/templates”和每个已安装应用程序的“templates”目录中进行搜索。

关于python - 需要有关 Django 教程的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4705962/

相关文章:

python - Scrapy 管道以正确的格式导出 csv 文件

javascript - 在现有管理面板的 ListView 中实现 JavaScript 图形应用程序

python - Django 休息框架以编程方式填充嵌套对象的字段

javascript - 点击 li 应该在其他 div 中打开一个 url 但有一些数据

django - sqlite数据库在推送到heroku服务器(django)时未迁移到postgresql

python - Django 呈现模板 `500.html` 而不是 `404.html`

python - 将开放曲线转换为有序像素列表 : a Python test code with numpy

python - 为什么我无法在 ubuntu 12.04 中将 python 连接到 firebird?

python - 如何使用 lxml 访问评论

python - Bokeh (Python): Format DateTime in Hover tooltip