8

I have been following the Django tutorial part 3 and am getting the following error when I attempt to view http://localhost:8000/polls/:

**Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>[0-9]+)/$']**

My files are as follows:

mysite/urls.py:

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

polls/urls.py:

from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

polls/detail.html:

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

polls/templates/polls/index.html:

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

What does this error mean?

How do I debug it?

Can you suggest a fix?

N.b. I have seen and tried the answers to the similar questions:

NoReverseMatch at /polls/ (django tutorial) Django 1.8.2 -- Tutorial Chapter 3 -- Error: NoReverseMatch at /polls/ -- Python 3.4.3 NoReverseMatch - Django 1.7 Beginners tutorial Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found https://groups.google.com/forum/#!msg/django-users/etSR78dgKBo/euSYcSyMCgAJ NoReverseMatch at /polls/ (django tutorial) https://www.reddit.com/r/django/comments/3d43gb/noreversematch_at_polls1results_in_django/

Edit, I initially missed the following question. Its excellent answer partially answers my question (how to debug) but does not cover my specific problem.

What is a NoReverseMatch error, and how do I fix it?

Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170
  • 1
    Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Aug 17 '16 at 13:13
  • The actual problem for this specific case is that `question.id` is empty – Sayse Aug 17 '16 at 13:14
  • @Sayse thanks, does that mean the error is expected and I'm just not supposed to be looking at http://localhost:8000/polls/ ? – Lee Aug 17 '16 at 13:32
  • 1
    Yes, since `question.id` is empty you can see you have an empty string argument` and you don't have a url that doesn't match that. so you need to work out why that id is missing – Sayse Aug 17 '16 at 13:33
  • @Sayse If you like, put that as an answer and I'll accept. – Lee Aug 17 '16 at 13:45
  • Thanks, but I'd think it would be better closed as a duplicate - One of the bullet points under Args and Kwargs in the duplicate points to that specific possible issue :) – Sayse Aug 17 '16 at 13:53
  • 1
    @Sayse Fair enough, thanks. – Lee Aug 17 '16 at 13:59
  • @Sayse I found that I am supposed to look at localhost:8000/polls, see my answer. – Lee Aug 17 '16 at 14:23

1 Answers1

9

This was the problem:

polls/templates/polls/index.html should have been:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

I had inadvertantly replaced the entire file with the following line, rather than just updating the relevant line (implied here):

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

As stated by @Sayse in the comments, this would mean that question.id is empty resulting in the error.

Lee
  • 29,398
  • 28
  • 117
  • 170