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 Django Templates Templates

ThatOneCoder -
ThatOneCoder -
9,310 Points

I seem to have problems with creating the course part of the website

So when I create the course part of my website I get this TemplateDoesNotExist at /courses/. Is It something to do with my course_list.html or something else? Here is my code for course_list.html: {% for course in courses %} h2>{{ course.title }}</h2> {{course.description }} {% endfor %} Here is my code for views.py:

from django.shortcuts import render

from .models import Course

from django.http import HttpResponse

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

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

You code looks correct-- good start! It just can't find your template in the expected folder.

However, Django has a sort of strict convention of template locations.

Since it appears you are working on the "courses" application, the actual folder to store the templates is:

courses/templates/courses

This may seem a little complicated, but there are several good reasons:

  1. Django organizational namespacing keeps templates inside of the apps but are disambiguated by the app name folder.
  2. Django apps can be made "pluggable"... that is an app can be plugged-in to another project with little or no modifications.
ThatOneCoder -
ThatOneCoder -
9,310 Points

So what your saying is I need rename the file? If yes, I can't Treehouse won't let me. Also Thanks for the great explanation!

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

In reality, you can put templates just about anywhere if you know how to override the defaults. But the defaults are very strategic for aforementioned reasons. And the Code Challenges are designed to run standard defaults.

Your code is good. The subfolder location is critical to the app finding the template. So in reality, I would not rename, but move the file.

if your views.py is this:

views.py

from django.shortcuts import render
from .models import Course

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

Then the location for the course_list.html file should be:

courses/templates/courses/course_list.html

The Django way of doing things---

suppose <PROJECT> is the top level folder of the Django project and <APP> is the folder of the current app.

The Django convention for APP templates is this--

<PROJECT>/<APP>/templates/<APP>/your_template_file.html

General global templates go at the top level-- like below.

<PROJECT>/templates/your_global_template.html

Not every app will necessarily use a global template, but it could. The problem being as a project grows, the more likely you will encounter name collisions.

ThatOneCoder -
ThatOneCoder -
9,310 Points

Thanks a bunch! I was stuck on here for while but I got it now! Your explanation was great!