Search code examples
djangodjango-templatesdjango-urlsurlconf

How do I fix my Django app's urls.py so I stop getting a TemplateDoesNotExist exception?


I am currently following along with a somewhat questionable Django tutorial called A complete blog engine using Django in 60 minutes and am stuck on page 6. So I have a Django project called blog and an inside I've an app called blogengine. Currently I am getting a TemplateDoesNotExist exception when I try loading

127.0.0.1:8000/blog/

Here is the full Traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/blog/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['blogengine',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/sez/blog/templates/blogengine/post_list.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/site-packages/django/contrib/admin/templates/blogengine/post_list.html (File does not exist)



Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/views/generic/list_detail.py" in object_list
  107.     t = template_loader.get_template(template_name)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
  157.     template, origin = find_template(template_name)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
  138.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /blog/
Exception Value: blogengine/post_list.html

In the Template Loader Error you can see Django tries to look for the post_list.html template in the /home/sez/blog/templates/blogengine/' directory. To get this working I have to make Django look in the/home/sez/blog/templates/blog/' directory but I still do not fully understand how URLconf works.

Below are my 2 url.py files. The first is my project level url.py

from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import direct_to_template
from blog.views import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
        (r'^admin/', include(admin.site.urls)),
        (r'^static/(.*)$',
            'django.views.static.serve',
            {'document_root': '/home/siddhion/blog/static/'}),
        (r'^(?P<template>\w+)$', direct_to_template),
        (r'^$', static_page, {'template':'base'}),
        (r'^blog/', include('blog.blogengine.urls')),
        url(r'^(?P<template>\w+)/$', static_page, name='static_page'),
        )

As I understand it, the line (r'^blog/', include('blog.blogengine.urls')), is telling redirecting control over to the url.py file in my blogengine directory. Here is the code for that file

from django.conf.urls.defaults import *
from django.views.generic import list_detail
from blog.blogengine.views import Post

urlpatterns = patterns('',
        url(r'^$',
            list_detail.object_list,
            {
                'queryset': Post.objects.all(),
                'template_object_name':'post',
            },
            name='blog_home'
            ),
    )

So how what edits would I have to make to my urls.py files in order to get Django to find and render the post_list.html template?


Solution

  • Assuming that the file /home/sez/blog/templates/blog/post_list.html exists, you only have to tell Django where to look for it. Apparently, it looks for your templates in /home/sez/blog/templates/blogengine/. You can change that by adjusting TEMPLATE_DIRS in your settings.py:

    TEMPLATE_DIRS = (
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        '/home/sez/blog/templates/blog',
    )
    

    However, it is common practice to name the template subdirectories as their corresponding apps, so you might want to consider moving your templates to blogengine, where Django assumes they are at the moment.