Home Download Documentation Weblog Community Code
Django documentationThis document is for Django's SVN release, which can be significantly different from previous releases. Get old docs here: Django 1.0
The Django template language?
About this document
This document explains the language syntax of the Django template system. If you¡¯re looking for a more technical perspective on how it works and how to extend it, see The Django template language: For Python programmers.
Django¡¯s template language is designed to strike a balance between power and ease. It¡¯s designed to feel comfortable to those used to working with HTML. If you have any exposure to other text-based template languages, such as Smarty or CheetahTemplate, you should feel right at home with Django¡¯s templates.
Philosophy
If you have a background in programming, or if you¡¯re used to languages like PHP which mix programming code directly into HTML, you¡¯ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.
The Django template system provides tags which function similarly to some programming constructs ¨C an if tag for boolean tests, a for tag for looping, etc. ¨C but these are not simply executed as the corresponding Python code, and the template system will not execute arbitrary Python expressions. Only the tags, filters and syntax listed below are supported by default (although you can add your own extensions to the template language as needed).
Templates?
A template is simply a text file. It can generate any text-based format (HTML, XML, CSV, etc.).
A template contains variables, which get replaced with values when the template is evaluated, and tags, which control the logic of the template.
Below is a minimal template that illustrates a few basics. Each element will be explained later in this document.:
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
<h1>{{ section.title }}</h1>
{% for story in story_list %}
<h2>
<a href="{{ story.get_absolute_url }}">
{{ story.headline|upper }}
</a>
</h2>
<p>{{ story.tease|truncatewords:"100" }}</p>
{% endfor %}
{% endblock %}
Philosophy
Why use a text-based template instead of an XML-based one (like Zope's TAL)? We wanted Django's template language to be usable for more than just XML/HTML templates. At World Online, we use it for e-mails, JavaScript and CSV. You can use the template language for any text-based format.
Oh, and one more thing: Making humans edit XML is sadistic!
Variables?
Variables look like this: {{ variable }}. When the template engine encounters a variable, it evaluates that variable and replaces it with the result.
Use a dot (.) to access attributes of a variable.
Behind the scenes
Technically, when the template system encounters a dot, it tries the following lookups, in this order:
Dictionary lookup
Attribute lookup
Method call
List-index lookup
In the above example, {{ section.title }} will be replaced with the title attribute of the section object.
If you use a variable that doesn't exist, the template system will insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is set to '' (the empty string) by default.