Reputation: 127
Attempting to start the server and getting following exception (starting server leads to polls/index.html page):
django.urls.exceptions.NoReverseMatch: Reverse for 'detail' with arguments '(1,)' not found. 1 pattern(s) tried: ['$(?P[0-9]+)']
If I remove the following line fron index.html, the error is removed. But I don't get what the issue is. Even the ide prompts the current data. Found similar questions where the issue was with incorrect number of arguments which isn't the case with this. Please help.
Error line:
{% url 'polls:detail' q.id %}
index.html
{% for q in latest_question_list %}
<li><a href="{% url 'polls:detail' q.id %}">{{ q.question_text }}</a></li>
{% endfor %}
urls.py (inside polls app folder)
app_name = 'polls'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)', views.detail, name='detail'),
]
views.py
def index(request):
latest_question_list = Question.objects.order_by('pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
def detail(request, question_id):
return HttpResponse(question_id)
urls.py (main)
urlpatterns = [
url(r'^$', include('polls.urls')),
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls'))
]
Upvotes: -1
Views: 357
Reputation: 15738
You have included your poll URLs into the regex with stopping character $ (urls.py - main)
url(r'^$', include('polls.urls'))
This way there is no possible fulfilment for the following regex
['$(?P[0-9]+)']
If you still want to show polls index as your index site remove $ from line
url(r'^', include('polls.urls'))
Also, you have 2 inclusions of the same namespace and first catch of it is used ( index )
You can even see this if you change namespace for the polls URL
url(r'^polls/', include('polls.urls', namespace='test'))
and use namespaced include as test:details ( this should work but I still recommend you to consolidate where you want to include poll URLs
Upvotes: 3