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

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' %}
      ">Courses</a>
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

thanks

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 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