【发布时间】:2025-11-28 15:45:01
【问题描述】:
我正在学习 Django 1.5 教程:编写你的第一个 Django 应用程序。在第 3 部分中,它讲授了如何加载名为 polls/index.html 的模板。它应该显示一个包含“What's up”的项目符号列表,当我指向“/polls/”的浏览器时,但是当我转到浏览器时
http://localhost:8000/polls/
,页面只是空白。
这是我的投票/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
这是我的投票/views.py
# Create your views here.
from django.http import HttpResponse
from django.template import RequestContext, loader
from polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = RequestContext(request, {
'latest_poll_list': latest_poll_list,
})
return HttpResponse(template.render(context))
def detail(request, poll_id):
return HttpResponse("You're looking at poll %s." % poll_id)
def results(request, poll_id):
return HttpResponse("You're looking at the results of poll %s." % poll_id)
def vote(request, poll_id):
return HttpResponse("You're voting on poll %s." % poll_id)
这是我的 index.html 的目录
mysite/polls/templates/polls/index.html
这是我的 index.html
{% if latest_poll_list %}
<ul>
{% for poll in latest_pol_list %}
<li><a href="/polls/{{poll.id}}">{{ poll.question }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
有人有同样的问题吗?
谢谢!!!
【问题讨论】:
-
你设置
TEMPLATE_DIRS了吗? -
latest_poll_list不是latest_pol_list -
谢谢大家!!!顺便说一句,任何 IDE 或某些方法都可以防止这种愚蠢的打字错误?
-
在测试时,您可以将
TEMPLATE_STRING_IF_INVALID设置为某个值,这样您就会收到有关模板中变量名不正确的警告。 django debug toolbar 也可用于调试。
标签: django