0

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?

blaze
  • 2,588
  • 3
  • 32
  • 47

1 Answers1

1

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 %}
iMom0
  • 12,493
  • 3
  • 49
  • 61
  • The strange thing is that polls.views.results doesn't work, but results does. – blaze Feb 16 '13 at 01:00
  • 1
    There is no views named `polls.views.results`, but you defined your url patterns with name `results`, more details https://docs.djangoproject.com/en/1.4/topics/http/urls/#naming-url-patterns – iMom0 Feb 16 '13 at 01:10