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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With