我的模板文件中有这个:
<a href="{% url polls.views.vote poll.id %}">Vote again?</a>
这是我的URLConf:
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
我更改了一些观点以使用泛型:
urlpatterns = patterns('polls.views',
url(r'^$',
ListView.as_view(
queryset=Poll.objects.order_by('-pub_date')[:5],
context_object_name='latest_poll_list',
template_name='polls/index.html')),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model=Poll,
template_name='polls/details.html')),
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/$', 'vote'),
)
现在显示此错误:
反转&#39; polls.views.results&#39;参数&#39;(1,)&#39;和关键字参数&#39; {}&#39;没找到。
我该如何解决这个问题?
答案 0 :(得分:1)
为您的网址格式添加名称:
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model=Poll,
template_name='polls/results.html'),
name='results'),
然后在模板中使用名称而不是视图名称:
{% url results poll.id %}