[Fixed]-Django error NoReverseMatch

1👍

Change your template url tag to

{% url 'course_detail' course.id %}

and in your urls.py

url(r'^(?P<pk>[0-9]+)/course/$', views.course_detail, name='course_detail')
👤Othman

0👍

Firstly, you have assigned reverse name for courses detail url as Course_detail in urls.py but you are using the reverse name as course_detail in your template. It should have been Course_detail in the template.

Secondly, you are passing pk as Course.pk in your template. It should have been course.pk instead as you are using course variable when iterating over courses objects.

Instead use reverse name with lowercase initial to remove the case-sensitive issues.

urls.py

from django.conf.urls import include, url
from Classes import views
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.classes_list, name='classes_list'),
    url(r'^(?P<pk>[0-9]+)/Course/$', views.course_detail, name='course_detail'), # assign reverse name with lowercase initial letter
    ] 

Then in your template, you can do this:

classes_list.html

{% extends "cacademy/base.html" %}
 {% load staticfiles %}
 {% block content %}
         <div>
             <h2> Remember you can only register 3 Courses at once!</h2>
         </div>
     {% for course in courses %}
         <div course="post">
                 <div course="date">
                     {{ course.started_date }}
                 </div>
             <h1><a href="{% url 'course_detail' pk=course.pk %}">{{ course.title }}</a></h1>
             <p>Taught by: {{ course.teachername|linebreaks }}</p>
         </div>
     {% endfor %}
 {% endblock content %}

Leave a comment