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

Jerimiah Willhite
Jerimiah Willhite
12,014 Points

NoReverseMatch help (step 2, course_list)

Hi! So I've gotten through changing the urls for the homepage in Dango 1.10.0 just fine, but now am stuck and not quite sure what to do to get the course_list url to function properly. I know other people have had this problem, but I haven't seen any questions/answers for changing the course_list URL.

Here's what my URL patterns looks like:

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

And when the link in the layout.html I'm trying is :

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

But this doesn't work. I've looked up how URLs work in Django but I'm not sure how to access the 'courses/views.py' instead of the 'root/views.py', or even if that's what I need to be doing. Do I need to be putting the path to the courses.view somewhere? If so, where? Sorry, I'm confused from looking up things for so long and could do with some help.

1 Answer

Hi I am not entrily sure of your question so apologies if I get this completley wrong.

If you are asking to show the list of courses in your template ?

if yes have you a urls.py with the following pattern under courses?

urlpatterns = [
    url(r'^$', views.course_list),
]

Also in your views.py have you created the course-list view?

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

As can be seen above the course_list renders the course_list.html template, however, you can change this to whta ever you want. To access the the list in the course_list function it renders the object courses to the template. So then you can access that object in the template :

{% for course in courses %}
   {{ course.title }}
   {{ course.description }} 
{% endfor %}

Hope this makes some sense and I answered some of your question :)