ScarletMcLearn2
ScarletMcLearn2

Reputation: 13

NoReverseMatch: Reverse for 'detail' not found. (Django Tutorials)

I've been going through the Django 2 tutorials.

I got the following error:

#Error:
#django.urls.exceptions.NoReverseMatch
#django.urls.exceptions.NoReverseMatch: Reverse for 'detail' not found. 'detail' is not a valid view function or pattern #name.

Did some googling and confirmed that I had named my view 'detail' and also had my app named.

Below are my codes. Please tell what is wrong. I am following the tutorial by heart, but this came up. How can I fix it keeping in par with the tutorials? Thank you!

Files: mysite/polls/templates/polls/index.html

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

mysite/polls/urls.py

app_name = 'polls'
urlpatterns = [
    path('', views.index, name='index'),
    # ex: /polls/
    # path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

mysite/polls/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)

Additional: mysite/urls.py

urlpatterns = [
    path('polls/', include('polls.urls', namespace='polls')),
    path('admin/', admin.site.urls),
]

Upvotes: 1

Views: 2010

Answers (2)

An Tran
An Tran

Reputation: 71

Remove from mysite/urls.py the namespace as you already specified app's app_name

or you can just remove the app_name and keep the namespace (not sure if this works in Django 2.0 as there are some tweaks in app_name and namespace in this version).

Upvotes: 1

Anurag Rana
Anurag Rana

Reputation: 1466

You haven't defined any function named as 'detail' in views.py file.

Add this code.

def detail(request, id):
    context = dict()
    return render(request, 'polls/index.html', context)

You have to add results and vote function as well.

Remove the commented lines from your index.html file. Syntax in these lines is not right and Django tries to parse commented lines as well before rendering.

Upvotes: 2

Related Questions