最近我开始学习 Django,很早就遇到了一个问题:在本教程的第 4 部分中,我将代码修改为通用视图后,详细信息站点看起来与以前不同。
我找不到重点,也没有显示错误声明。
我已经发布了下面的代码。
民意调查/urls.py:
from polls.models import Poll
urlpatterns =patterns('',
url(r'$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html'),
name='index'),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/detail.html'),
name='detail'),
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model=Poll,
template_name='polls/results.html'),
name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote', name='vote'),
)
索引.html
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href = "{% url 'polls:detail' poll.id %}">{{poll.question}}</a></li>
{% endfor %}
{% else %}
<p> No polls are available.</p>
{% endif %}
详细信息.html
<h1>{{poll.question}}</h1>
{%if error_message %}<p><strong>{{error_message}}</strong></p>{%endif%}
<form action = '{% url 'polls:vote' poll.id %}' method = 'post'>
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type = 'radio' name = 'choice' id = 'choice{{forloop.counter}}' value = '{{choice.id}}'/>
<lable for = 'choice{{forloop.counter}}'>{{choice.choice_text}}</lable><br/>
{%endfor%}
<input type ='submit' value = 'Vote'/>
</form>