NoReverseMatch at /polls/1/

114 views
Skip to first unread message

jahan

unread,
Oct 4, 2015, 12:25:08 PM10/4/15
to Django users
Hi Django group,

I'm following the tutorial and get stuck on part 3, Removing hardcoded URLs in templates

I read through the topics that discuss this, but can't find the issue in my case.

my detail.html template looks like

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
   
<li><a href="{% url 'detail' question.id %}">{{ choice.choice_text }}</a></li>
{% endfor %}
</ul>


in my urls.py for the polls project I have

urlpatterns = [
     url
(r'^$', 'index', name="index"),
     url
(r'^(?P<question_id>\d+)/$', views.detail, name="detail"),
     url
(r'^(?P<question_id>\d+)/results/$', 'results', name="results"),
     url
(r'^(?P<poll_id>\d+)/vote/$', 'vote', name="vote"),
]

Thanks for any suggestion,
jack

James Schneider

unread,
Oct 4, 2015, 3:04:48 PM10/4/15
to django...@googlegroups.com

Can you paste the entire error?

Also, read through the error page, it should show you which line is causing the problem.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/298b2f49-8374-4e54-bfd3-639a7de3af7f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jahan

unread,
Oct 4, 2015, 3:35:08 PM10/4/15
to Django users
Hi James,

This is the error

NoReverseMatch at /polls/1/

Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://localhost:8000/polls/1/
Django Version: 1.8.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 496
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/var/www/django/mysite',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-client',
 '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
 '/usr/lib/python2.7/dist-packages/ubuntuone-couch',
 '/usr/lib/python2.7/dist-packages/ubuntuone-installer',
 '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
Server time: Sun, 4 Oct 2015 19:31:13 +0000

Error during template rendering

In template /var/www/django/mysite/polls/templates/polls/detail.html, error at line 4

Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

1 <h1>{{ question.question_text }}</h1>
2 <ul>
3
{% for choice in question.choice_set.all %}
4
<li><a href=" {% url 'detail' question.id %} ">{{ choice.choice_text }}</a></li>
5 {% endfor %}
6 </ul>
7

James Schneider

unread,
Oct 4, 2015, 3:43:46 PM10/4/15
to django...@googlegroups.com

My guess it's that your polls/urls.py file is not being processed/included properly since none of your URL patterns were even tried.

What does your mysite/urls.py file look like?

-James

jahan

unread,
Oct 4, 2015, 4:05:13 PM10/4/15
to Django users
This is my mysite/url,py

from django.contrib import admin

urlpatterns
= [
    url
(r'^admin/', include(admin.site.urls)),
    url
(r'^polls/', include('polls.urls',namespace = "polls")),
].

James Schneider

unread,
Oct 4, 2015, 5:03:03 PM10/4/15
to django...@googlegroups.com

> This is my mysite/url,py
>
> from django.contrib import admin
>
> urlpatterns = [
>     url(r'^admin/', include(admin.site.urls)),
>     url(r'^polls/', include('polls.urls',namespace = "polls")),
> ].
>

Oh, yep, there's the problem. You added a namespace called 'polls' but you aren't referencing it in your template, so the URL resolver isn't using your polls/urls.py file for that {% url %} call. You probably forgot to update the {% url %} tag after the tutorial portion that added the namespace to the mysite/urls.py file.

You need to update your {% url %} tag to use 'polls:detail' rather than just 'detail'. 

See the last code box on that tutorial page: https://docs.djangoproject.com/en/1.8/intro/tutorial03/#namespacing-url-names

-James

monoBOT

unread,
Oct 5, 2015, 4:51:29 AM10/5/15
to django...@googlegroups.com
The problem is that you dont have any url that has the poll id as parameter. 
So django cant find it

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

lipt0n

unread,
Oct 5, 2015, 10:01:14 AM10/5/15
to Django users
just remove ,namespace = "polls"

jahan

unread,
Oct 5, 2015, 3:33:06 PM10/5/15
to Django users
Hi James,

That was it! thanks a bunch for pointing that out.

cheers,
Jack
Reply all
Reply to author
Forward
0 new messages