Python Django教程4,修改views.py后细节站点缺少投票操作?
最近我开始学习Django,很早就遇到了一个问题: 在本教程的第4部分中,我将代码修改为通用视图后,详细信息站点看起来与以前不同 我找不到要点,也没有显示错误声明 我已经把代码贴在下面了 polls/url.py:Python Django教程4,修改views.py后细节站点缺少投票操作?,python,django,Python,Django,最近我开始学习Django,很早就遇到了一个问题: 在本教程的第4部分中,我将代码修改为通用视图后,详细信息站点看起来与以前不同 我找不到要点,也没有显示错误声明 我已经把代码贴在下面了 polls/url.py: from polls.models import Poll urlpatterns =patterns('', url(r'$', ListView.as_view( queryset=Poll.objects.order_by('-
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'),
)
从polls.models导入Poll
urlpatterns=模式(“”,
url(r“$”,
ListView.as_视图(
queryset=Poll.objects.order_by('-pub_date')[:5],
context\u object\u name='latest\u poll\u list',
模板_name='polls/index.html'),
name='index'),
url(r'^(?P\d+/$),
DetailView.as\u视图(
模型=投票,
模板_name='polls/detail.html'),
name='detail'),
url(r'^(?P\d+)/results/$”,
DetailView.as\u视图(
模型=投票,
模板_name='polls/results.html'),
name='results'),
url(r'^(?P\d+)/vote/$,'polls.views.vote',name='vote'),
)
index.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 %}
<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>
{%if-latest\u poll\u list%}
{最新轮询列表%中的轮询百分比}
{%endfor%}
{%else%}
没有投票
{%endif%}
detail.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 %}
<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>
{{poll.question}
{%if error\u message%}{{{error\u message}{%endif%}
{%csrf_令牌%}
{poll.choice_set.all%}
{{choice.choice_text}}
{%endfor%}
轮询/url.py
第一个url的正则表达式匹配应该是r'^$'
urlpatterns =patterns('',
url(r'^$',
ListView.as_view(....
这对我来说真的很奇怪,我是一个潜心的初学者。如果我没有清楚地描述这个问题,请告诉我…@zds\u cn而不知道对你来说有什么区别,你能验证所有的视图都指向正确的页面吗?可能您以前看到的页面不正确。