Templates in a django project

A smart way of placing templates in your django project.

So you’re starting a new project in Django? One thing you’ll obviously want are templates and you’ll have to decide where to put them.

The Default Way

The method I used for a long time is the method described in Django Tutorial and used for example in Pinax. It is based on templates folder(s) which is placed somewhere in your filesystem, most often directly in your project’s root.

- project
    -apps
        -app1
        -app2
    -templates
        -app1 (templates for app1)
        -app2 (templates for app2)
    -other folders/files

To get it working some configuration in settings.py is also required:

# Configure Django to first load templates from filesystem
# and then from application directories
# These are default settings
TEMPLATE_LOADERS = [
    "django.template.loaders.filesystem.Loader",
    "django.template.loaders.app_directories.Loader",
 ]

# Define where exactly Django should look for templates
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]

You can read more about settings used above here.

With this simple config you can create a theme for your project. Just start creating templates in templates directory. It is important to remember that it is prefferable to place templates in subdirectories to keep templates for specific apps separated.

However there are inconveniences with this configuration:

  • First is about templatetags – if you need a tag to render something project specific where should you place it’s code? You need to create a dedicated application for this case.
  • Second is about i18n – to generate locales only for templates in Django < 1.2 it was required to manually move apps folders out of project directory, then call bin/django makessages -l pl (or whatever lang you use) and after that move apps back to project directory. Why? Because makemessages browses all folders under current folder so it would generate locales also for applications from apps. The good news is that Django 1.2 has provided --ignore option to makemessages that allows exclusion of some directories while generating messagefiles.

The LFC Way

The method I use now is based on a LFC‘s way of doing things. It is very simple and doesn’t have inconveniences of previous method.

The main concept here is an application. Your theme is just an ordinary application, so instead of defining TEMPLATE_DIRS it is enough to add yourproject_theme app to INSTALLED_APPS.

Django searches for templates in apps specified in INSTALLED_APPS browsing them in order of definition so it is important to place yourproject_theme application before any other applications. For example:

INSTALLED_APPS = (
    "lfc_theme",
    "django.contrib.admin",
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    "django.contrib.flatpages",
    "django.contrib.sitemaps",
    "django.contrib.comments",
    "django_extensions",
    "lfc",
    "lfc_blog",
    "portlets",
    "tagging",
    "contact_form",
    "pagination",
    "workflows",
    "permissions",
    "gunicorn",
)

Main advantages of having a theme as an application are that you can have locales, templatetags and even static files as well as project specific utility functions in one place – your theme application.

Curious about example? Look into LFC Theme.

Summary

Both methods are simple to use but the second has advantages and IMHO results in a cleaner structure of a project.

30. June 2010 by restless_being
Categories: Uncategorized | Tags: , , | Leave a comment

Leave a Reply

Required fields are marked *