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.


url namespace for courses fails in layout.html python django

all looks good but it is not getting the namespace correctly

error on line 14 of the layout.html

<a href="
      {% url 'courses:list' %}
from django.conf.urls import include, url
from django.contrib import admin, flatpages
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from . import views

urlpatterns = [
    url(r'^courses/', include('courses.urls', namespace='courses')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.home),
    url(r'^home', views.home),

urlpatterns += staticfiles_urlpatterns()

Restarted the python manage.py run server did not help

can you also show your views file


Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

Three debug pieces needed: the complete error message, the files urls.py and the layout.html. The extra spaces between the quotes and the url template tag might be an issue:

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

Sometimes issues are caused by the line *preceding * the marked link. Check the previous statement.

1 Answer

from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render

from .models import Course, Step

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

def course_detail(request, pk):
    # course = Course.objects.get(pk=pk)
    course = get_object_or_404(Course, pk=pk)
    return render(request, 'courses/course_detail.html', {'courses': courses})

def step_detail(request, course_pk, step_pk):
    step = get_object_or_404(Step, course_id=course_pk, pk=step_pk)
    return render(request, 'courses/step_detail.html', {'step': step})

hope that helps, thanks in advance

Thanks this is tricky stuff... I think I corrected the issue here by eliminating the previous manual url to home