Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Final Details url Tag

Error during template rendering "NoReverseMatch at /courses/"

I've been following along precisely with the videos and everything has worked so far but at 6:24 I enter the code as Kenneth did and get the error in the title. I can't figure out what went wrong. More specifically the error is:

Error during template rendering

In template /home/treehouse/workspace/learning_site/courses/templates/courses/course_list.html, error at line 9
Reverse for 'detail' with arguments '()' and keyword arguments '{'pk': 1}' not found. 0 pattern(s) tried: []
1   {% extends "layout.html" %}
2   
3   {% block title %}Available Courses{% endblock %}
4   
5   {% block content %}
6   <div class="cards">
7           {% for course in courses %}
8           <div class="card">
9   
                  <header><a href="
      {% url 'detail' pk=course.pk %}
      ">{{ course.title }}</a></header>

10                  <div class="card-copy">
11                    {{ course.description }}
12                  </div>
13          </div>
14          {% endfor %}
15    </div>
16  
17  {% endblock %}

[MOD: wrapped codeblock in ```html+jinja formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Look at the URL definition for 'detail'. The error message says your are calling this URL with the keyword argument "pk=1". Does your URL have a (?P<pk>) in it? The named group in the URL needs to match the keyword used in the template tag.

Chris thanks for your attention. Yes the URL has (?P<pk>) (<.p.k.> keeps being formatted out of this for some reason) in it. It's the last url in urlpatterns in courses app's urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.course_list),

    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/', views.step_detail),
    url(r'(?P<pk>\d+)/', views.course_detail),
]

and here's the relevant view in views.py:

def course_detail(request, pk):
    course = get_object_or_404(Course, pk=pk)
    return render(request, 
                  'courses/course_detail.html', {'course': course})
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The error seems to be with the view course_list not course_detail.

What view calls course_list.html?

Chris, this is the view for course_list.html:

def course_list(request):
    courses = Course.objects.all()
    return render(request, 'courses/course_list.html', {'courses': courses})

Also, I edited my comment a few times and you responded quickly (thanks!) but can you please confirm that you are responding to my comment in its final form?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

In order to do a reverse URL lookup from a template, the URL needs to have a name:

url(r'(?P<pk>\d+)/', views.course_detail, name='detail'),

Perfect, that fixed it! Thanks Chris!