NoReverseMatch at /polls/

436 views
Skip to first unread message

voger

unread,
Sep 7, 2013, 4:13:27 PM9/7/13
to django...@googlegroups.com
Hi everyone. I am trying to walk through the polls tutorial. I am stuck at the part 4 after the change of the views from function based to class based. A search in Google brought plenty results but none of them seems to be related exactly to this situation or maybe it is over my head to understand what is going on.
When I visit http://127.0.0.1:8000/polls/ i get

NoReverseMatch at /polls/

Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.5.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
...

In templateοΏ½/home/voger/PycharmProjects/mysite/polls/templates/polls/index.html, error at lineοΏ½4

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

1 {% if latest_poll_list %}
2 <ul>
3 {% for poll in latest_poll_list %}
4 <li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
5 {% endfor %}
6 </ul>
7 {% else %}
8 <p>No polls are available.</p>
9 {% endif %}

The offending line is line number 4. The classes used are

class IndexView(generic.ListView):
οΏ½οΏ½οΏ½ template_name = 'polls/index.html'
οΏ½οΏ½οΏ½ context_object_name = 'latest_poll_list'

οΏ½οΏ½οΏ½ def get_queryset(self):
οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ """Return the last five published polls."""
οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ return Poll.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
οΏ½οΏ½οΏ½ model = Poll
οΏ½οΏ½οΏ½ template_name = 'polls/detail.html'

and in /polls/urls.py the urls are

urlpatterns = patterns('',
οΏ½οΏ½οΏ½ url(r'^$', views.IndexView.as_view(), name='index'),
οΏ½οΏ½οΏ½ url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
οΏ½οΏ½οΏ½ url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
οΏ½οΏ½οΏ½ url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)

Can someone please explain what is wrong?

voger

unread,
Sep 7, 2013, 4:29:06 PM9/7/13
to django...@googlegroups.com
The previous message was delivered as a garbage due to html formating. I
am sending it again as plain text.


Hi everyone. I am trying to walk through the polls tutorial. I am stuck
at the part 4 after the change of the views from function based to class
based. A search in Google brought plenty results but none of them seems
to be related exactly to this situation or maybe it is over my head to
understand what is going on.
When I visit http://127.0.0.1:8000/polls/ i get

NoReverseMatch at /polls/

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

Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 1.5.2
Exception Type: NoReverseMatch
Exception Value:

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

...

In template
/home/voger/PycharmProjects/mysite/polls/templates/polls/index.html,
error at line 4
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}'
not found.
1 {% if latest_poll_list %}
2 <ul>
3 {% for poll in latest_poll_list %}
4 <li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>
5 {% endfor %}
6 </ul>
7 {% else %}
8 <p>No polls are available.</p>
9 {% endif %}

The offending line is line number 4. The classes used are

class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'

def get_queryset(self):
"""Return the last five published polls."""
return Poll.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
model = Poll
template_name = 'polls/detail.html'

and in /polls/urls.py the urls are

urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(),
name='results'),

voger

unread,
Sep 7, 2013, 6:29:00 PM9/7/13
to django...@googlegroups.com
Problem solved. I missed a step in the tutorial where name spacing in
the root URLconf was discussed. I still don't understand it completely
but the bottom line is that the line number 4 in the file index.html
should be replaced with

<li><a href="{% url 'polls:detail' poll.id %}">{{ poll.question }}</a></li>
Reply all
Reply to author
Forward
0 new messages