سؤال

I have this in my template file:

<a href="{% url polls.views.vote poll.id %}">Vote again?</a>

This was my 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'),
)

I changed some views to use generics:

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'),
)

And now this error is shown:

Reverse for 'polls.views.results' with arguments '(1,)' and keyword arguments '{}' not found.

How can I fix this?

هل كانت مفيدة؟

المحلول

Add name to your url pattern:

url(r'^(?P<pk>\d+)/results/$',
    DetailView.as_view(
        model=Poll,
        template_name='polls/results.html'),
    name='results'),

then in templates use name instead of view name:

{% url results poll.id %}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top