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 trialwilldenn
1,552 PointsError 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
Treehouse Moderator 68,454 PointsLook 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.
willdenn
1,552 Pointswilldenn
1,552 PointsChris 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:and here's the relevant view in views.py:
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsThe error seems to be with the view course_list not course_detail.
What view calls course_list.html?
willdenn
1,552 Pointswilldenn
1,552 PointsChris, this is the view for course_list.html:
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
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsIn 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'),
willdenn
1,552 Pointswilldenn
1,552 PointsPerfect, that fixed it! Thanks Chris!