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

Konrad Hunter
Konrad Hunter
3,629 Points

NoReverseMatch at /courses/1/2/

I get this error when I add the line. <a href="{% url 'courses.views.course_list' %}">Courses</a>

It runs fine with only line. <a href="{% url 'views.hello_world' %}">Home</a>

It looks like workspaces is running django version 1.9.9.

I know others have had this question, but haven't found a working solution from other answers. I appreciate the help.

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Konrad,

This was a tricky one to debug, and I learned something about Django in the process. The main issue is that once you assign a name or a namespace to a url pattern, Django is going to be looking for that particular "namespace:name" when it tries to match patterns. You won't be able to call the view by chaining together it's location, (eg. "courses.views.course_list"). I didn't know that Django enforced this.

In your learning_site/urls.py, you have actually copied and pasted the same code twice. The second instance of "urlpatterns" overrides the first, and in the second you have defined the courses' namespace as "courses".

In courses/urls.py, you have named the call to the "course_list" view as "list".

So in layout.html when you are adding the anchor tag, you need to refer to the view by its 'namespace:name'.

Eg.

<a href="{% url 'courses:list' %}">Courses</a>

As of your current workspace, it looks like you forgot to include the namespace.

If you want to try it with your original "href" attribute that you mentioned in your question, you will need to delete the namespace and name in both learning_site/urls.py, and courses/urls.py.

In the video, after Kenneth assigns the names and namespace to the url patterns, he changes the hrefs accordingly.

Hope this helps.

Konrad Hunter
Konrad Hunter
3,629 Points

Thanks, Ryan. It worked as you said.