Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Sam Fellows
Sam Fellows
3,968 Points

"NoReverseMatch at /courses/" Local Instance, have read through threads but am still stumped.

I am working on a local instance of Django (v 1.10) and following along with the course. I am at the point where I need to have a link to my "course details". I have followed the instructions and read through the forums but I am still receiving the following error:

"NoReverseMatch at /courses/"

The guidance in the forums suggested that you create a name for your URL. This worked for my main navigation but does not seem to work on the individual course detail URLs. Below please find the code I am using for my URLs and template files. If anyone had a similar issue or has guidance it would be most appreciated. Thanks!

urls.py (in app directory):

urlpatterns = [ url(r'^$', views.course_list, name="list"), url(r'(?P<pk>\d+)/$', views.course_detail, name="step"), url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail, name="detail"), ]

course_lists.html (template file in app):

<a href="{% url 'course:detail' %}">{{ course.title}}</a>

2 Answers

Ryan S
Ryan S
27,275 Points

Hi Sam,

Did you namespace the 'courses' app in the main project's urls.py? If so, then I believe Kenneth is using 'courses' (plural) as the namespace. In your template, you are calling a singular 'course' namespace.

# learning_site/urls.py
urlpatterns = [
    url(r'^courses/', include('courses.urls', namespace='courses')),
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.hello_world, name='hello'),
]

# course_list.html
<a href="{% url 'courses:detail' %}">{{ course.title}}</a>

There are a few other issues as well:

You have named your url's backwards. "step_detail" should be named "step", and "course_detail" should be named "detail". So even if your namespace is correct, you are calling the wrong view.

Also, you should make sure the order of your url regex's matches those in the video. I believe Kenneth lists them in this order: course_list, step_detail, course_detail. Since Django goes through the list in order to try and match a pattern, this prevents it from stopping at the first matched group name. In your case, "course_detail" might be matched before "step_detail", even if you were trying to match "step_detail".

So it should look like this:

# courses/urls.py
urlpatterns = [
    url(r'^$', views.course_list, name="list"), 
    url(r'(?P<course_pk>\d+)/(?P<step_pk>\d+)/$', views.step_detail, name="step"),
    url(r'(?P<pk>\d+)/$', views.course_detail, name="detail"),
]

And I'm not sure if this is just a typo in your question, but the template file should be named "course_list.html" (singular), not "course_lists.html".

Hope this helps.

Sam Fellows
Sam Fellows
3,968 Points

Ryan,

Thank you for taking a look and for pointing out the problem with my courses/urls.py file. In my Learning Site url.py file I did include a namespace. I implemented the changes you suggested and am still getting an error unfortunately. Specifically:

In template /learning_site/sams_learning_site/courses/templates/courses/course_lists.html, error at line 10

<a href="{% url 'courses:detail' %}">{{ course.title}}</a>

Thank you again for your time, I appreciate you taking a look. If you are still willing to help perhaps I could share the project with you? I am really stumped.

Ryan S
Ryan S
27,275 Points

Hey Sam,

Sure, no problem. I'll take a look at it.

Sam Fellows
Sam Fellows
3,968 Points

Thanks Ryan. Whats the best way to get ahold of you, are you on GIT?

Ryan S
Ryan S
27,275 Points

I am. If you send me your github handle I can fork your project and go from there.