| 1 |
================================================== |
|---|
| 2 |
The Django template language: For template authors |
|---|
| 3 |
================================================== |
|---|
| 4 |
|
|---|
| 5 |
.. admonition:: About this document |
|---|
| 6 |
|
|---|
| 7 |
This document explains the language syntax of the Django template system. If |
|---|
| 8 |
you're looking for a more technical perspective on how it works and how to |
|---|
| 9 |
extend it, see `The Django template language: For Python programmers`_. |
|---|
| 10 |
|
|---|
| 11 |
Django's template language is designed to strike a balance between power and |
|---|
| 12 |
ease. It's designed to feel comfortable to those used to working with HTML. If |
|---|
| 13 |
you have any exposure to other text-based template languages, such as Smarty_ |
|---|
| 14 |
or CheetahTemplate_, you should feel right at home with Django's templates. |
|---|
| 15 |
|
|---|
| 16 |
.. admonition:: Philosophy |
|---|
| 17 |
|
|---|
| 18 |
If you have a background in programming, or if you're used to languages |
|---|
| 19 |
like PHP which mix programming code directly into HTML, you'll want to |
|---|
| 20 |
bear in mind that the Django template system is not simply Python embedded |
|---|
| 21 |
into HTML. This is by design: the template system is meant to express |
|---|
| 22 |
presentation, not program logic. |
|---|
| 23 |
|
|---|
| 24 |
The Django template system provides tags which function similarly to some |
|---|
| 25 |
programming constructs -- an ``{% if %}`` tag for boolean tests, a ``{% |
|---|
| 26 |
for %}`` tag for looping, etc. -- but these are not simply executed as the |
|---|
| 27 |
corresponding Python code, and the template system will not execute |
|---|
| 28 |
arbitrary Python expressions. Only the tags, filters and syntax listed |
|---|
| 29 |
below are supported by default (although you can add `your own |
|---|
| 30 |
extensions`_ to the template language as needed). |
|---|
| 31 |
|
|---|
| 32 |
.. _`The Django template language: For Python programmers`: ../templates_python/ |
|---|
| 33 |
.. _Smarty: http://smarty.php.net/ |
|---|
| 34 |
.. _CheetahTemplate: http://www.cheetahtemplate.org/ |
|---|
| 35 |
.. _your own extensions: ../templates_python/#extending-the-template-system |
|---|
| 36 |
|
|---|
| 37 |
Templates |
|---|
| 38 |
========= |
|---|
| 39 |
|
|---|
| 40 |
A template is simply a text file. It can generate any text-based format (HTML, |
|---|
| 41 |
XML, CSV, etc.). |
|---|
| 42 |
|
|---|
| 43 |
A template contains **variables**, which get replaced with values when the |
|---|
| 44 |
template is evaluated, and **tags**, which control the logic of the template. |
|---|
| 45 |
|
|---|
| 46 |
Below is a minimal template that illustrates a few basics. Each element will be |
|---|
| 47 |
explained later in this document.:: |
|---|
| 48 |
|
|---|
| 49 |
{% extends "base_generic.html" %} |
|---|
| 50 |
|
|---|
| 51 |
{% block title %}{{ section.title }}{% endblock %} |
|---|
| 52 |
|
|---|
| 53 |
{% block content %} |
|---|
| 54 |
<h1>{{ section.title }}</h1> |
|---|
| 55 |
|
|---|
| 56 |
{% for story in story_list %} |
|---|
| 57 |
<h2> |
|---|
| 58 |
<a href="{{ story.get_absolute_url }}"> |
|---|
| 59 |
{{ story.headline|upper }} |
|---|
| 60 |
</a> |
|---|
| 61 |
</h2> |
|---|
| 62 |
<p>{{ story.tease|truncatewords:"100" }}</p> |
|---|
| 63 |
{% endfor %} |
|---|
| 64 |
{% endblock %} |
|---|
| 65 |
|
|---|
| 66 |
.. admonition:: Philosophy |
|---|
| 67 |
|
|---|
| 68 |
Why use a text-based template instead of an XML-based one (like Zope's |
|---|
| 69 |
TAL)? We wanted Django's template language to be usable for more than |
|---|
| 70 |
just XML/HTML templates. At World Online, we use it for e-mails, |
|---|
| 71 |
JavaScript and CSV. You can use the template language for any text-based |
|---|
| 72 |
format. |
|---|
| 73 |
|
|---|
| 74 |
Oh, and one more thing: Making humans edit XML is sadistic! |
|---|
| 75 |
|
|---|
| 76 |
Variables |
|---|
| 77 |
========= |
|---|
| 78 |
|
|---|
| 79 |
Variables look like this: ``{{ variable }}``. When the template engine |
|---|
| 80 |
encounters a variable, it evaluates that variable and replaces it with the |
|---|
| 81 |
result. |
|---|
| 82 |
|
|---|
| 83 |
Use a dot (``.``) to access attributes of a variable. |
|---|
| 84 |
|
|---|
| 85 |
.. admonition:: Behind the scenes |
|---|
| 86 |
|
|---|
| 87 |
Technically, when the template system encounters a dot, it tries the |
|---|
| 88 |
following lookups, in this order: |
|---|
| 89 |
|
|---|
| 90 |
* Dictionary lookup |
|---|
| 91 |
* Attribute lookup |
|---|
| 92 |
* Method call |
|---|
| 93 |
* List-index lookup |
|---|
| 94 |
|
|---|
| 95 |
In the above example, ``{{ section.title }}`` will be replaced with the |
|---|
| 96 |
``title`` attribute of the ``section`` object. |
|---|
| 97 |
|
|---|
| 98 |
If you use a variable that doesn't exist, the template system will insert |
|---|
| 99 |
the value of the ``TEMPLATE_STRING_IF_INVALID`` setting, which is set to ``''`` |
|---|
| 100 |
(the empty string) by default. |
|---|
| 101 |
|
|---|
| 102 |
See `Using the built-in reference`_, below, for help on finding what variables |
|---|
| 103 |
are available in a given template. |
|---|
| 104 |
|
|---|
| 105 |
Filters |
|---|
| 106 |
======= |
|---|
| 107 |
|
|---|
| 108 |
You can modify variables for display by using **filters**. |
|---|
| 109 |
|
|---|
| 110 |
Filters look like this: ``{{ name|lower }}``. This displays the value of the |
|---|
| 111 |
``{{ name }}`` variable after being filtered through the ``lower`` filter, |
|---|
| 112 |
which converts text to lowercase. Use a pipe (``|``) to apply a filter. |
|---|
| 113 |
|
|---|
| 114 |
Filters can be "chained." The output of one filter is applied to the next. |
|---|
| 115 |
``{{ text|escape|linebreaks }}`` is a common idiom for escaping text contents, |
|---|
| 116 |
then converting line breaks to ``<p>`` tags. |
|---|
| 117 |
|
|---|
| 118 |
Some filters take arguments. A filter argument looks like this: ``{{ |
|---|
| 119 |
bio|truncatewords:30 }}``. This will display the first 30 words of the ``bio`` |
|---|
| 120 |
variable. |
|---|
| 121 |
|
|---|
| 122 |
Filter arguments that contain spaces must be quoted; for example, to join a list |
|---|
| 123 |
with commas and spaced you'd use ``{{ list|join:", " }}``. |
|---|
| 124 |
|
|---|
| 125 |
The `Built-in filter reference`_ below describes all the built-in filters. |
|---|
| 126 |
|
|---|
| 127 |
Tags |
|---|
| 128 |
==== |
|---|
| 129 |
|
|---|
| 130 |
Tags look like this: ``{% tag %}``. Tags are more complex than variables: Some |
|---|
| 131 |
create text in the output, some control flow by performing loops or logic, and |
|---|
| 132 |
some load external information into the template to be used by later variables. |
|---|
| 133 |
|
|---|
| 134 |
Some tags require beginning and ending tags (i.e. |
|---|
| 135 |
``{% tag %} ... tag contents ... {% endtag %}``). The `Built-in tag reference`_ |
|---|
| 136 |
below describes all the built-in tags. You can create your own tags, if you |
|---|
| 137 |
know how to write Python code. |
|---|
| 138 |
|
|---|
| 139 |
Comments |
|---|
| 140 |
======== |
|---|
| 141 |
|
|---|
| 142 |
To comment-out part of a line in a template, use the comment syntax: ``{# #}``. |
|---|
| 143 |
|
|---|
| 144 |
For example, this template would render as ``'hello'``:: |
|---|
| 145 |
|
|---|
| 146 |
{# greeting #}hello |
|---|
| 147 |
|
|---|
| 148 |
A comment can contain any template code, invalid or not. For example:: |
|---|
| 149 |
|
|---|
| 150 |
{# {% if foo %}bar{% else %} #} |
|---|
| 151 |
|
|---|
| 152 |
This syntax can only be used for single-line comments (no newlines are |
|---|
| 153 |
permitted between the ``{#`` and ``#}`` delimiters). If you need to comment |
|---|
| 154 |
out a multiline portion of the template, see the ``comment`` tag, below__. |
|---|
| 155 |
|
|---|
| 156 |
__ comment_ |
|---|
| 157 |
|
|---|
| 158 |
Template inheritance |
|---|
| 159 |
==================== |
|---|
| 160 |
|
|---|
| 161 |
The most powerful -- and thus the most complex -- part of Django's template |
|---|
| 162 |
engine is template inheritance. Template inheritance allows you to build a base |
|---|
| 163 |
"skeleton" template that contains all the common elements of your site and |
|---|
| 164 |
defines **blocks** that child templates can override. |
|---|
| 165 |
|
|---|
| 166 |
It's easiest to understand template inheritance by starting with an example:: |
|---|
| 167 |
|
|---|
| 168 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| 169 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 170 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 171 |
<head> |
|---|
| 172 |
<link rel="stylesheet" href="style.css" /> |
|---|
| 173 |
<title>{% block title %}My amazing site{% endblock %}</title> |
|---|
| 174 |
</head> |
|---|
| 175 |
|
|---|
| 176 |
<body> |
|---|
| 177 |
<div id="sidebar"> |
|---|
| 178 |
{% block sidebar %} |
|---|
| 179 |
<ul> |
|---|
| 180 |
<li><a href="/">Home</a></li> |
|---|
| 181 |
<li><a href="/blog/">Blog</a></li> |
|---|
| 182 |
</ul> |
|---|
| 183 |
{% endblock %} |
|---|
| 184 |
</div> |
|---|
| 185 |
|
|---|
| 186 |
<div id="content"> |
|---|
| 187 |
{% block content %}{% endblock %} |
|---|
| 188 |
</div> |
|---|
| 189 |
</body> |
|---|
| 190 |
</html> |
|---|
| 191 |
|
|---|
| 192 |
This template, which we'll call ``base.html``, defines a simple HTML skeleton |
|---|
| 193 |
document that you might use for a simple two-column page. It's the job of |
|---|
| 194 |
"child" templates to fill the empty blocks with content. |
|---|
| 195 |
|
|---|
| 196 |
In this example, the ``{% block %}`` tag defines three blocks that child |
|---|
| 197 |
templates can fill in. All the ``block`` tag does is to tell the template |
|---|
| 198 |
engine that a child template may override those portions of the template. |
|---|
| 199 |
|
|---|
| 200 |
A child template might look like this:: |
|---|
| 201 |
|
|---|
| 202 |
{% extends "base.html" %} |
|---|
| 203 |
|
|---|
| 204 |
{% block title %}My amazing blog{% endblock %} |
|---|
| 205 |
|
|---|
| 206 |
{% block content %} |
|---|
| 207 |
{% for entry in blog_entries %} |
|---|
| 208 |
<h2>{{ entry.title }}</h2> |
|---|
| 209 |
<p>{{ entry.body }}</p> |
|---|
| 210 |
{% endfor %} |
|---|
| 211 |
{% endblock %} |
|---|
| 212 |
|
|---|
| 213 |
The ``{% extends %}`` tag is the key here. It tells the template engine that |
|---|
| 214 |
this template "extends" another template. When the template system evaluates |
|---|
| 215 |
this template, first it locates the parent -- in this case, "base.html". |
|---|
| 216 |
|
|---|
| 217 |
At that point, the template engine will notice the three ``{% block %}`` tags |
|---|
| 218 |
in ``base.html`` and replace those blocks with the contents of the child |
|---|
| 219 |
template. Depending on the value of ``blog_entries``, the output might look |
|---|
| 220 |
like:: |
|---|
| 221 |
|
|---|
| 222 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
|---|
| 223 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|---|
| 224 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
|---|
| 225 |
<head> |
|---|
| 226 |
<link rel="stylesheet" href="style.css" /> |
|---|
| 227 |
<title>My amazing blog</title> |
|---|
| 228 |
</head> |
|---|
| 229 |
|
|---|
| 230 |
<body> |
|---|
| 231 |
<div id="sidebar"> |
|---|
| 232 |
<ul> |
|---|
| 233 |
<li><a href="/">Home</a></li> |
|---|
| 234 |
<li><a href="/blog/">Blog</a></li> |
|---|
| 235 |
</ul> |
|---|
| 236 |
</div> |
|---|
| 237 |
|
|---|
| 238 |
<div id="content"> |
|---|
| 239 |
<h2>Entry one</h2> |
|---|
| 240 |
<p>This is my first entry.</p> |
|---|
| 241 |
|
|---|
| 242 |
<h2>Entry two</h2> |
|---|
| 243 |
<p>This is my second entry.</p> |
|---|
| 244 |
</div> |
|---|
| 245 |
</body> |
|---|
| 246 |
</html> |
|---|
| 247 |
|
|---|
| 248 |
Note that since the child template didn't define the ``sidebar`` block, the |
|---|
| 249 |
value from the parent template is used instead. Content within a ``{% block %}`` |
|---|
| 250 |
tag in a parent template is always used as a fallback. |
|---|
| 251 |
|
|---|
| 252 |
You can use as many levels of inheritance as needed. One common way of using |
|---|
| 253 |
inheritance is the following three-level approach: |
|---|
| 254 |
|
|---|
| 255 |
* Create a ``base.html`` template that holds the main look-and-feel of your |
|---|
| 256 |
site. |
|---|
| 257 |
* Create a ``base_SECTIONNAME.html`` template for each "section" of your |
|---|
| 258 |
site. For example, ``base_news.html``, ``base_sports.html``. These |
|---|
| 259 |
templates all extend ``base.html`` and include section-specific |
|---|
| 260 |
styles/design. |
|---|
| 261 |
* Create individual templates for each type of page, such as a news |
|---|
| 262 |
article or blog entry. These templates extend the appropriate section |
|---|
| 263 |
template. |
|---|
| 264 |
|
|---|
| 265 |
This approach maximizes code reuse and makes it easy to add items to shared |
|---|
| 266 |
content areas, such as section-wide navigation. |
|---|
| 267 |
|
|---|
| 268 |
Here are some tips for working with inheritance: |
|---|
| 269 |
|
|---|
| 270 |
* If you use ``{% extends %}`` in a template, it must be the first template |
|---|
| 271 |
tag in that template. Template inheritance won't work, otherwise. |
|---|
| 272 |
|
|---|
| 273 |
* More ``{% block %}`` tags in your base templates are better. Remember, |
|---|
| 274 |
child templates don't have to define all parent blocks, so you can fill |
|---|
| 275 |
in reasonable defaults in a number of blocks, then only define the ones |
|---|
| 276 |
you need later. It's better to have more hooks than fewer hooks. |
|---|
| 277 |
|
|---|
| 278 |
* If you find yourself duplicating content in a number of templates, it |
|---|
| 279 |
probably means you should move that content to a ``{% block %}`` in a |
|---|
| 280 |
parent template. |
|---|
| 281 |
|
|---|
| 282 |
* If you need to get the content of the block from the parent template, |
|---|
| 283 |
the ``{{ block.super }}`` variable will do the trick. This is useful |
|---|
| 284 |
if you want to add to the contents of a parent block instead of |
|---|
| 285 |
completely overriding it. Data inserted using ``{{ block.super }}`` will |
|---|
| 286 |
not be automatically escaped (see the `next section`_), since it was |
|---|
| 287 |
already escaped, if necessary, in the parent template. |
|---|
| 288 |
|
|---|
| 289 |
* For extra readability, you can optionally give a *name* to your |
|---|
| 290 |
``{% endblock %}`` tag. For example:: |
|---|
| 291 |
|
|---|
| 292 |
{% block content %} |
|---|
| 293 |
... |
|---|
| 294 |
{% endblock content %} |
|---|
| 295 |
|
|---|
| 296 |
In larger templates, this technique helps you see which ``{% block %}`` |
|---|
| 297 |
tags are being closed. |
|---|
| 298 |
|
|---|
| 299 |
Finally, note that you can't define multiple ``{% block %}`` tags with the same |
|---|
| 300 |
name in the same template. This limitation exists because a block tag works in |
|---|
| 301 |
"both" directions. That is, a block tag doesn't just provide a hole to fill -- |
|---|
| 302 |
it also defines the content that fills the hole in the *parent*. If there were |
|---|
| 303 |
two similarly-named ``{% block %}`` tags in a template, that template's parent |
|---|
| 304 |
wouldn't know which one of the blocks' content to use. |
|---|
| 305 |
|
|---|
| 306 |
.. _next section: #automatic-html-escaping |
|---|
| 307 |
|
|---|
| 308 |
Automatic HTML escaping |
|---|
| 309 |
======================= |
|---|
| 310 |
|
|---|
| 311 |
**New in Django development version** |
|---|
| 312 |
|
|---|
| 313 |
When generating HTML from templates, there's always a risk that a variable will |
|---|
| 314 |
include characters that affect the resulting HTML. For example, consider this |
|---|
| 315 |
template fragment:: |
|---|
| 316 |
|
|---|
| 317 |
Hello, {{ name }}. |
|---|
| 318 |
|
|---|
| 319 |
At first, this seems like a harmless way to display a user's name, but consider |
|---|
| 320 |
what would happen if the user entered his name as this:: |
|---|
| 321 |
|
|---|
| 322 |
<script>alert('hello')</script> |
|---|
| 323 |
|
|---|
| 324 |
With this name value, the template would be rendered as:: |
|---|
| 325 |
|
|---|
| 326 |
Hello, <script>alert('hello')</script> |
|---|
| 327 |
|
|---|
| 328 |
...which means the browser would pop-up a JavaScript alert box! |
|---|
| 329 |
|
|---|
| 330 |
Similarly, what if the name contained a ``'<'`` symbol, like this? |
|---|
| 331 |
|
|---|
| 332 |
<b>username |
|---|
| 333 |
|
|---|
| 334 |
That would result in a rendered template like this:: |
|---|
| 335 |
|
|---|
| 336 |
Hello, <b>username |
|---|
| 337 |
|
|---|
| 338 |
...which, in turn, would result in the remainder of the Web page being bolded! |
|---|
| 339 |
|
|---|
| 340 |
Clearly, user-submitted data shouldn't be trusted blindly and inserted directly |
|---|
| 341 |
into your Web pages, because a malicious user could use this kind of hole to |
|---|
| 342 |
do potentially bad things. This type of security exploit is called a |
|---|
| 343 |
`Cross Site Scripting`_ (XSS) attack. |
|---|
| 344 |
|
|---|
| 345 |
To avoid this problem, you have two options: |
|---|
| 346 |
|
|---|
| 347 |
* One, you can make sure to run each untrusted variable through the |
|---|
| 348 |
``escape`` filter (documented below), which converts potentially harmful |
|---|
| 349 |
HTML characters to unharmful ones. This was default the default solution |
|---|
| 350 |
in Django for its first few years, but the problem is that it puts the |
|---|
| 351 |
onus on *you*, the developer / template author, to ensure you're escaping |
|---|
| 352 |
everything. It's easy to forget to escape data. |
|---|
| 353 |
|
|---|
| 354 |
* Two, you can take advantage of Django's automatic HTML escaping. The |
|---|
| 355 |
remainder of this section describes how auto-escaping works. |
|---|
| 356 |
|
|---|
| 357 |
By default in the Django development version, every template automatically |
|---|
| 358 |
escapes the output of every variable tag. Specifically, these five characters |
|---|
| 359 |
are escaped: |
|---|
| 360 |
|
|---|
| 361 |
* ``<`` is converted to ``<`` |
|---|
| 362 |
* ``>`` is converted to ``>`` |
|---|
| 363 |
* ``'`` (single quote) is converted to ``'`` |
|---|
| 364 |
* ``"`` (double quote) is converted to ``"`` |
|---|
| 365 |
* ``&`` is converted to ``&`` |
|---|
| 366 |
|
|---|
| 367 |
Again, we stress that this behavior is on by default. If you're using Django's |
|---|
| 368 |
template system, you're protected. |
|---|
| 369 |
|
|---|
| 370 |
.. _Cross Site Scripting: http://en.wikipedia.org/wiki/Cross-site_scripting |
|---|
| 371 |
|
|---|
| 372 |
How to turn it off |
|---|
| 373 |
------------------ |
|---|
| 374 |
|
|---|
| 375 |
If you don't want data to be auto-escaped, on a per-site, per-template level or |
|---|
| 376 |
per-variable level, you can turn it off in several ways. |
|---|
| 377 |
|
|---|
| 378 |
Why would you want to turn it off? Because sometimes, template variables |
|---|
| 379 |
contain data that you *intend* to be rendered as raw HTML, in which case you |
|---|
| 380 |
don't want their contents to be escaped. For example, you might store a blob of |
|---|
| 381 |
HTML in your database and want to embed that directly into your template. Or, |
|---|
| 382 |
you might be using Django's template system to produce text that is *not* HTML |
|---|
| 383 |
-- like an e-mail message, for instance. |
|---|
| 384 |
|
|---|
| 385 |
For individual variables |
|---|
| 386 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 387 |
|
|---|
| 388 |
To disable auto-escaping for an individual variable, use the ``safe`` filter:: |
|---|
| 389 |
|
|---|
| 390 |
This will be escaped: {{ data }} |
|---|
| 391 |
This will not be escaped: {{ data|safe }} |
|---|
| 392 |
|
|---|
| 393 |
Think of *safe* as shorthand for *safe from further escaping* or *can be |
|---|
| 394 |
safely interpreted as HTML*. In this example, if ``data`` contains ``'<b>'``, |
|---|
| 395 |
the output will be:: |
|---|
| 396 |
|
|---|
| 397 |
This will be escaped: <b> |
|---|
| 398 |
This will not be escaped: <b> |
|---|
| 399 |
|
|---|
| 400 |
For template blocks |
|---|
| 401 |
~~~~~~~~~~~~~~~~~~~ |
|---|
| 402 |
|
|---|
| 403 |
To control auto-escaping for a template, wrap the template (or just a |
|---|
| 404 |
particular section of the template) in the ``autoescape`` tag, like so:: |
|---|
| 405 |
|
|---|
| 406 |
{% autoescape off %} |
|---|
| 407 |
Hello {{ name }} |
|---|
| 408 |
{% endautoescape %} |
|---|
| 409 |
|
|---|
| 410 |
The ``autoescape`` tag takes either ``on`` or ``off`` as its argument. At |
|---|
| 411 |
times, you might want to force auto-escaping when it would otherwise be |
|---|
| 412 |
disabled. Here is an example template:: |
|---|
| 413 |
|
|---|
| 414 |
Auto-escaping is on by default. Hello {{ name }} |
|---|
| 415 |
|
|---|
| 416 |
{% autoescape off %} |
|---|
| 417 |
This will not be auto-escaped: {{ data }}. |
|---|
| 418 |
|
|---|
| 419 |
Nor this: {{ other_data }} |
|---|
| 420 |
{% autoescape on %} |
|---|
| 421 |
Auto-escaping applies again: {{ name }} |
|---|
| 422 |
{% endautoescape %} |
|---|
| 423 |
{% endautoescape %} |
|---|
| 424 |
|
|---|
| 425 |
The auto-escaping tag passes its effect onto templates that extend the |
|---|
| 426 |
current one as well as templates included via the ``include`` tag, just like |
|---|
| 427 |
all block tags. For example:: |
|---|
| 428 |
|
|---|
| 429 |
# base.html |
|---|
| 430 |
|
|---|
| 431 |
{% autoescape off %} |
|---|
| 432 |
<h1>{% block title %}{% endblock %}</h1> |
|---|
| 433 |
{% block content %} |
|---|
| 434 |
{% endblock %} |
|---|
| 435 |
{% endautoescape %} |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
# child.html |
|---|
| 439 |
|
|---|
| 440 |
{% extends "base.html" %} |
|---|
| 441 |
{% block title %}This & that{% endblock %} |
|---|
| 442 |
{% block content %}{{ greeting }}{% endblock %} |
|---|
| 443 |
|
|---|
| 444 |
Because auto-escaping is turned off in the base template, it will also be |
|---|
| 445 |
turned off in the child template, resulting in the following rendered |
|---|
| 446 |
HTML when the ``greeting`` variable contains the string ``<b>Hello!</b>``:: |
|---|
| 447 |
|
|---|
| 448 |
<h1>This & that</h1> |
|---|
| 449 |
<b>Hello!</b> |
|---|
| 450 |
|
|---|
| 451 |
Notes |
|---|
| 452 |
----- |
|---|
| 453 |
|
|---|
| 454 |
Generally, template authors don't need to worry about auto-escaping very much. |
|---|
| 455 |
Developers on the Python side (people writing views and custom filters) need to |
|---|
| 456 |
think about the cases in which data shouldn't be escaped, and mark data |
|---|
| 457 |
appropriately, so things Just Work in the template. |
|---|
| 458 |
|
|---|
| 459 |
If you're creating a template that might be used in situations where you're |
|---|
| 460 |
not sure whether auto-escaping is enabled, then add an ``escape`` filter to any |
|---|
| 461 |
variable that needs escaping. When auto-escaping is on, there's no danger of |
|---|
| 462 |
the ``escape`` filter *double-escaping* data -- the ``escape`` filter does not |
|---|
| 463 |
affect auto-escaped variables. |
|---|
| 464 |
|
|---|
| 465 |
String literals and automatic escaping |
|---|
| 466 |
-------------------------------------- |
|---|
| 467 |
|
|---|
| 468 |
As we mentioned earlier, filter arguments can be strings:: |
|---|
| 469 |
|
|---|
| 470 |
{{ data|default:"This is a string literal." }} |
|---|
| 471 |
|
|---|
| 472 |
All string literals are inserted **without** any automatic escaping into the |
|---|
| 473 |
template -- they act as if they were all passed through the ``safe`` filter. |
|---|
| 474 |
The reasoning behind this is that the template author is in control of what |
|---|
| 475 |
goes into the string literal, so they can make sure the text is correctly |
|---|
| 476 |
escaped when the template is written. |
|---|
| 477 |
|
|---|
| 478 |
This means you would write :: |
|---|
| 479 |
|
|---|
| 480 |
{{ data|default:"3 > 2" }} |
|---|
| 481 |
|
|---|
| 482 |
...rather than :: |
|---|
| 483 |
|
|---|
| 484 |
{{ data|default:"3 > 2" }} <-- Bad! Don't do this. |
|---|
| 485 |
|
|---|
| 486 |
This doesn't affect what happens to data coming from the variable itself. |
|---|
| 487 |
The variable's contents are still automatically escaped, if necessary, because |
|---|
| 488 |
they're beyond the control of the template author. |
|---|
| 489 |
|
|---|
| 490 |
Using the built-in reference |
|---|
| 491 |
============================ |
|---|
| 492 |
|
|---|
| 493 |
Django's admin interface includes a complete reference of all template tags and |
|---|
| 494 |
filters available for a given site. To see it, go to your admin interface and |
|---|
| 495 |
click the "Documentation" link in the upper right of the page. |
|---|
| 496 |
|
|---|
| 497 |
The reference is divided into 4 sections: tags, filters, models, and views. |
|---|
| 498 |
|
|---|
| 499 |
The **tags** and **filters** sections describe all the built-in tags (in fact, |
|---|
| 500 |
the tag and filter references below come directly from those pages) as well as |
|---|
| 501 |
any custom tag or filter libraries available. |
|---|
| 502 |
|
|---|
| 503 |
The **views** page is the most valuable. Each URL in your site has a separate |
|---|
| 504 |
entry here, and clicking on a URL will show you: |
|---|
| 505 |
|
|---|
| 506 |
* The name of the view function that generates that view. |
|---|
| 507 |
* A short description of what the view does. |
|---|
| 508 |
* The **context**, or a list of variables available in the view's template. |
|---|
| 509 |
* The name of the template or templates that are used for that view. |
|---|
| 510 |
|
|---|
| 511 |
Each view documentation page also has a bookmarklet that you can use to jump |
|---|
| 512 |
from any page to the documentation page for that view. |
|---|
| 513 |
|
|---|
| 514 |
Because Django-powered sites usually use database objects, the **models** |
|---|
| 515 |
section of the documentation page describes each type of object in the system |
|---|
| 516 |
along with all the fields available on that object. |
|---|
| 517 |
|
|---|
| 518 |
Taken together, the documentation pages should tell you every tag, filter, |
|---|
| 519 |
variable and object available to you in a given template. |
|---|
| 520 |
|
|---|
| 521 |
Custom tag and filter libraries |
|---|
| 522 |
=============================== |
|---|
| 523 |
|
|---|
| 524 |
Certain applications provide custom tag and filter libraries. To access them in |
|---|
| 525 |
a template, use the ``{% load %}`` tag:: |
|---|
| 526 |
|
|---|
| 527 |
{% load comments %} |
|---|
| 528 |
|
|---|
| 529 |
{% comment_form for blogs.entries entry.id with is_public yes %} |
|---|
| 530 |
|
|---|
| 531 |
In the above, the ``load`` tag loads the ``comments`` tag library, which then |
|---|
| 532 |
makes the ``comment_form`` tag available for use. Consult the documentation |
|---|
| 533 |
area in your admin to find the list of custom libraries in your installation. |
|---|
| 534 |
|
|---|
| 535 |
The ``{% load %}`` tag can take multiple library names, separated by spaces. |
|---|
| 536 |
Example:: |
|---|
| 537 |
|
|---|
| 538 |
{% load comments i18n %} |
|---|
| 539 |
|
|---|
| 540 |
Custom libraries and template inheritance |
|---|
| 541 |
----------------------------------------- |
|---|
| 542 |
|
|---|
| 543 |
When you load a custom tag or filter library, the tags/filters are only made |
|---|
| 544 |
available to the current template -- not any parent or child templates along |
|---|
| 545 |
the template-inheritance path. |
|---|
| 546 |
|
|---|
| 547 |
For example, if a template ``foo.html`` has ``{% load comments %}``, a child |
|---|
| 548 |
template (e.g., one that has ``{% extends "foo.html" %}``) will *not* have |
|---|
| 549 |
access to the comments template tags and filters. The child template is |
|---|
| 550 |
responsible for its own ``{% load comments %}``. |
|---|
| 551 |
|
|---|
| 552 |
This is a feature for the sake of maintainability and sanity. |
|---|
| 553 |
|
|---|
| 554 |
Built-in tag and filter reference |
|---|
| 555 |
================================= |
|---|
| 556 |
|
|---|
| 557 |
For those without an admin site available, reference for the stock tags and |
|---|
| 558 |
filters follows. Because Django is highly customizable, the reference in your |
|---|
| 559 |
admin should be considered the final word on what tags and filters are |
|---|
| 560 |
available, and what they do. |
|---|
| 561 |
|
|---|
| 562 |
Built-in tag reference |
|---|
| 563 |
---------------------- |
|---|
| 564 |
|
|---|
| 565 |
autoescape |
|---|
| 566 |
~~~~~~~~~~ |
|---|
| 567 |
|
|---|
| 568 |
**New in Django development version** |
|---|
| 569 |
|
|---|
| 570 |
Control the current auto-escaping behavior. This tag takes either ``on`` or |
|---|
| 571 |
``off`` as an argument and that determines whether auto-escaping is in effect |
|---|
| 572 |
inside the block. |
|---|
| 573 |
|
|---|
| 574 |
When auto-escaping is in effect, all variable content has HTML escaping applied |
|---|
| 575 |
to it before placing the result into the output (but after any filters have |
|---|
| 576 |
been applied). This is equivalent to manually applying the ``escape`` filter |
|---|
| 577 |
to each variable. |
|---|
| 578 |
|
|---|
| 579 |
The only exceptions are variables that are already marked as "safe" from |
|---|
| 580 |
escaping, either by the code that populated the variable, or because it has had |
|---|
| 581 |
the ``safe`` or ``escape`` filters applied. |
|---|
| 582 |
|
|---|
| 583 |
block |
|---|
| 584 |
~~~~~ |
|---|
| 585 |
|
|---|
| 586 |
Define a block that can be overridden by child templates. See |
|---|
| 587 |
`Template inheritance`_ for more information. |
|---|
| 588 |
|
|---|
| 589 |
comment |
|---|
| 590 |
~~~~~~~ |
|---|
| 591 |
|
|---|
| 592 |
Ignore everything between ``{% comment %}`` and ``{% endcomment %}`` |
|---|
| 593 |
|
|---|
| 594 |
cycle |
|---|
| 595 |
~~~~~ |
|---|
| 596 |
|
|---|
| 597 |
**Changed in Django development version** |
|---|
| 598 |
Cycle among the given strings or variables each time this tag is encountered. |
|---|
| 599 |
|
|---|
| 600 |
Within a loop, cycles among the given strings/variables each time through the |
|---|
| 601 |
loop:: |
|---|
| 602 |
|
|---|
| 603 |
{% for o in some_list %} |
|---|
| 604 |
<tr class="{% cycle 'row1' 'row2' rowvar %}"> |
|---|
| 605 |
... |
|---|
| 606 |
</tr> |
|---|
| 607 |
{% endfor %} |
|---|
| 608 |
|
|---|
| 609 |
Outside of a loop, give the values a unique name the first time you call it, |
|---|
| 610 |
then use that name each successive time through:: |
|---|
| 611 |
|
|---|
| 612 |
<tr class="{% cycle 'row1' 'row2' rowvar as rowcolors %}">...</tr> |
|---|
| 613 |
<tr class="{% cycle rowcolors %}">...</tr> |
|---|
| 614 |
<tr class="{% cycle rowcolors %}">...</tr> |
|---|
| 615 |
|
|---|
| 616 |
You can use any number of values, separated by spaces. Values enclosed in |
|---|
| 617 |
single (') or double quotes (") are treated as string literals, while values |
|---|
| 618 |
without quotes are assumed to refer to context variables. |
|---|
| 619 |
|
|---|
| 620 |
You can also separate values with commas:: |
|---|
| 621 |
|
|---|
| 622 |
{% cycle row1,row2,row3 %} |
|---|
| 623 |
|
|---|
| 624 |
In this syntax, each value will be interpreted as literal text. The |
|---|
| 625 |
comma-based syntax exists for backwards-compatibility, and should not be |
|---|
| 626 |
used for new projects. |
|---|
| 627 |
|
|---|
| 628 |
debug |
|---|
| 629 |
~~~~~ |
|---|
| 630 |
|
|---|
| 631 |
Output a whole load of debugging information, including the current context and |
|---|
| 632 |
imported modules. |
|---|
| 633 |
|
|---|
| 634 |
extends |
|---|
| 635 |
~~~~~~~ |
|---|
| 636 |
|
|---|
| 637 |
Signal that this template extends a parent template. |
|---|
| 638 |
|
|---|
| 639 |
This tag can be used in two ways: |
|---|
| 640 |
|
|---|
| 641 |
* ``{% extends "base.html" %}`` (with quotes) uses the literal value |
|---|
| 642 |
``"base.html"`` as the name of the parent template to extend. |
|---|
| 643 |
|
|---|
| 644 |
* ``{% extends variable %}`` uses the value of ``variable``. If the variable |
|---|
| 645 |
evaluates to a string, Django will use that string as the name of the |
|---|
| 646 |
parent template. If the variable evaluates to a ``Template`` object, |
|---|
| 647 |
Django will use that object as the parent template. |
|---|
| 648 |
|
|---|
| 649 |
See `Template inheritance`_ for more information. |
|---|
| 650 |
|
|---|
| 651 |
filter |
|---|
| 652 |
~~~~~~ |
|---|
| 653 |
|
|---|
| 654 |
Filter the contents of the variable through variable filters. |
|---|
| 655 |
|
|---|
| 656 |
Filters can also be piped through each other, and they can have arguments -- |
|---|
| 657 |
just like in variable syntax. |
|---|
| 658 |
|
|---|
| 659 |
Sample usage:: |
|---|
| 660 |
|
|---|
| 661 |
{% filter force_escape|lower %} |
|---|
| 662 |
This text will be HTML-escaped, and will appear in all lowercase. |
|---|
| 663 |
{% endfilter %} |
|---|
| 664 |
|
|---|
| 665 |
firstof |
|---|
| 666 |
~~~~~~~ |
|---|
| 667 |
|
|---|
| 668 |
Outputs the first variable passed that is not False. Outputs nothing if all the |
|---|
| 669 |
passed variables are False. |
|---|
| 670 |
|
|---|
| 671 |
Sample usage:: |
|---|
| 672 |
|
|---|
| 673 |
{% firstof var1 var2 var3 %} |
|---|
| 674 |
|
|---|
| 675 |
This is equivalent to:: |
|---|
| 676 |
|
|---|
| 677 |
{% if var1 %} |
|---|
| 678 |
{{ var1 }} |
|---|
| 679 |
{% else %}{% if var2 %} |
|---|
| 680 |
{{ var2 }} |
|---|
| 681 |
{% else %}{% if var3 %} |
|---|
| 682 |
{{ var3 }} |
|---|
| 683 |
{% endif %}{% endif %}{% endif %} |
|---|
| 684 |
|
|---|
| 685 |
You can also use a literal string as a fallback value in case all |
|---|
| 686 |
passed variables are False:: |
|---|
| 687 |
|
|---|
| 688 |
{% firstof var1 var2 var3 "fallback value" %} |
|---|
| 689 |
|
|---|
| 690 |
for |
|---|
| 691 |
~~~ |
|---|
| 692 |
|
|---|
| 693 |
Loop over each item in an array. For example, to display a list of athletes |
|---|
| 694 |
provided in ``athlete_list``:: |
|---|
| 695 |
|
|---|
| 696 |
<ul> |
|---|
| 697 |
{% for athlete in athlete_list %} |
|---|
| 698 |
<li>{{ athlete.name }}</li> |
|---|
| 699 |
{% endfor %} |
|---|
| 700 |
</ul> |
|---|
| 701 |
|
|---|
| 702 |
You can loop over a list in reverse by using ``{% for obj in list reversed %}``. |
|---|
| 703 |
|
|---|
| 704 |
**New in Django development version** |
|---|
| 705 |
If you need to loop over a list of lists, you can unpack the values |
|---|
| 706 |
in eachs sub-list into a set of known names. For example, if your context contains |
|---|
| 707 |
a list of (x,y) coordinates called ``points``, you could use the following |
|---|
| 708 |
to output the list of points:: |
|---|
| 709 |
|
|---|
| 710 |
{% for x, y in points %} |
|---|
| 711 |
There is a point at {{ x }},{{ y }} |
|---|
| 712 |
{% endfor %} |
|---|
| 713 |
|
|---|
| 714 |
This can also be useful if you need to access the items in a dictionary. |
|---|
| 715 |
For example, if your context contained a dictionary ``data``, the following |
|---|
| 716 |
would display the keys and values of the dictionary:: |
|---|
| 717 |
|
|---|
| 718 |
{% for key, value in data.items %} |
|---|
| 719 |
{{ key }}: {{ value }} |
|---|
| 720 |
{% endfor %} |
|---|
| 721 |
|
|---|
| 722 |
The for loop sets a number of variables available within the loop: |
|---|
| 723 |
|
|---|
| 724 |
========================== ================================================ |
|---|
| 725 |
Variable Description |
|---|
| 726 |
========================== ================================================ |
|---|
| 727 |
``forloop.counter`` The current iteration of the loop (1-indexed) |
|---|
| 728 |
``forloop.counter0`` The current iteration of the loop (0-indexed) |
|---|
| 729 |
``forloop.revcounter`` The number of iterations from the end of the |
|---|
| 730 |
loop (1-indexed) |
|---|
| 731 |
``forloop.revcounter0`` The number of iterations from the end of the |
|---|
| 732 |
loop (0-indexed) |
|---|
| 733 |
``forloop.first`` True if this is the first time through the loop |
|---|
| 734 |
``forloop.last`` True if this is the last time through the loop |
|---|
| 735 |
``forloop.parentloop`` For nested loops, this is the loop "above" the |
|---|
| 736 |
current one |
|---|
| 737 |
========================== ================================================ |
|---|
| 738 |
|
|---|
| 739 |
if |
|---|
| 740 |
~~ |
|---|
| 741 |
|
|---|
| 742 |
The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e. |
|---|
| 743 |
exists, is not empty, and is not a false boolean value) the contents of the |
|---|
| 744 |
block are output:: |
|---|
| 745 |
|
|---|
| 746 |
{% if athlete_list %} |
|---|
| 747 |
Number of athletes: {{ athlete_list|length }} |
|---|
| 748 |
{% else %} |
|---|
| 749 |
No athletes. |
|---|
| 750 |
{% endif %} |
|---|
| 751 |
|
|---|
| 752 |
In the above, if ``athlete_list`` is not empty, the number of athletes will be |
|---|
| 753 |
displayed by the ``{{ athlete_list|length }}`` variable. |
|---|
| 754 |
|
|---|
| 755 |
As you can see, the ``if`` tag can take an optional ``{% else %}`` clause that |
|---|
| 756 |
will be displayed if the test fails. |
|---|
| 757 |
|
|---|
| 758 |
``if`` tags may use ``and``, ``or`` or ``not`` to test a number of variables or |
|---|
| 759 |
to negate a given variable:: |
|---|
| 760 |
|
|---|
| 761 |
{% if athlete_list and coach_list %} |
|---|
| 762 |
Both athletes and coaches are available. |
|---|
| 763 |
{% endif %} |
|---|
| 764 |
|
|---|
| 765 |
{% if not athlete_list %} |
|---|
| 766 |
There are no athletes. |
|---|
| 767 |
{% endif %} |
|---|
| 768 |
|
|---|
| 769 |
{% if athlete_list or coach_list %} |
|---|
| 770 |
There are some athletes or some coaches. |
|---|
| 771 |
{% endif %} |
|---|
| 772 |
|
|---|
| 773 |
{% if not athlete_list or coach_list %} |
|---|
| 774 |
There are no athletes or there are some coaches (OK, so |
|---|
| 775 |
writing English translations of boolean logic sounds |
|---|
| 776 |
stupid; it's not our fault). |
|---|
| 777 |
{% endif %} |
|---|
| 778 |
|
|---|
| 779 |
{% if athlete_list and not coach_list %} |
|---|
| 780 |
There are some athletes and absolutely no coaches. |
|---|
| 781 |
{% endif %} |
|---|
| 782 |
|
|---|
| 783 |
``if`` tags don't allow ``and`` and ``or`` clauses within the same tag, because |
|---|
| 784 |
the order of logic would be ambiguous. For example, this is invalid:: |
|---|
| 785 |
|
|---|
| 786 |
{% if athlete_list and coach_list or cheerleader_list %} |
|---|
| 787 |
|
|---|
| 788 |
If you need to combine ``and`` and ``or`` to do advanced logic, just use nested |
|---|
| 789 |
``if`` tags. For example:: |
|---|
| 790 |
|
|---|
| 791 |
{% if athlete_list %} |
|---|
| 792 |
{% if coach_list or cheerleader_list %} |
|---|
| 793 |
We have athletes, and either coaches or cheerleaders! |
|---|
| 794 |
{% endif %} |
|---|
| 795 |
{% endif %} |
|---|
| 796 |
|
|---|
| 797 |
Multiple uses of the same logical operator are fine, as long as you use the |
|---|
| 798 |
same operator. For example, this is valid:: |
|---|
| 799 |
|
|---|
| 800 |
{% if athlete_list or coach_list or parent_list or teacher_list %} |
|---|
| 801 |
|
|---|
| 802 |
ifchanged |
|---|
| 803 |
~~~~~~~~~ |
|---|
| 804 |
|
|---|
| 805 |
Check if a value has changed from the last iteration of a loop. |
|---|
| 806 |
|
|---|
| 807 |
The 'ifchanged' block tag is used within a loop. It has two possible uses. |
|---|
| 808 |
|
|---|
| 809 |
1. Checks its own rendered contents against its previous state and only |
|---|
| 810 |
displays the content if it has changed. For example, this displays a list of |
|---|
| 811 |
days, only displaying the month if it changes:: |
|---|
| 812 |
|
|---|
| 813 |
<h1>Archive for {{ year }}</h1> |
|---|
| 814 |
|
|---|
| 815 |
{% for date in days %} |
|---|
| 816 |
{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %} |
|---|
| 817 |
<a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a> |
|---|
| 818 |
{% endfor %} |
|---|
| 819 |
|
|---|
| 820 |
2. If given a variable, check whether that variable has changed. For |
|---|
| 821 |
example, the following shows the date every time it changes, but |
|---|
| 822 |
only shows the hour if both the hour and the date has changed:: |
|---|
| 823 |
|
|---|
| 824 |
{% for date in days %} |
|---|
| 825 |
{% ifchanged date.date %} {{ date.date }} {% endifchanged %} |
|---|
| 826 |
{% ifchanged date.hour date.date %} |
|---|
| 827 |
{{ date.hour }} |
|---|
| 828 |
{% endifchanged %} |
|---|
| 829 |
{% endfor %} |
|---|
| 830 |
|
|---|
| 831 |
ifequal |
|---|
| 832 |
~~~~~~~ |
|---|
| 833 |
|
|---|
| 834 |
Output the contents of the block if the two arguments equal each other. |
|---|
| 835 |
|
|---|
| 836 |
Example:: |
|---|
| 837 |
|
|---|
| 838 |
{% ifequal user.id comment.user_id %} |
|---|
| 839 |
... |
|---|
| 840 |
{% endifequal %} |
|---|
| 841 |
|
|---|
| 842 |
As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional. |
|---|
| 843 |
|
|---|
| 844 |
The arguments can be hard-coded strings, so the following is valid:: |
|---|
| 845 |
|
|---|
| 846 |
{% ifequal user.username "adrian" %} |
|---|
| 847 |
... |
|---|
| 848 |
{% endifequal %} |
|---|
| 849 |
|
|---|
| 850 |
It is only possible to compare an argument to template variables or strings. |
|---|
| 851 |
You cannot check for equality with Python objects such as ``True`` or |
|---|
| 852 |
``False``. If you need to test if something is true or false, use the ``if`` |
|---|
| 853 |
tag instead. |
|---|
| 854 |
|
|---|
| 855 |
ifnotequal |
|---|
| 856 |
~~~~~~~~~~ |
|---|
| 857 |
|
|---|
| 858 |
Just like ``ifequal``, except it tests that the two arguments are not equal. |
|---|
| 859 |
|
|---|
| 860 |
include |
|---|
| 861 |
~~~~~~~ |
|---|
| 862 |
|
|---|
| 863 |
Loads a template and renders it with the current context. This is a way of |
|---|
| 864 |
"including" other templates within a template. |
|---|
| 865 |
|
|---|
| 866 |
The template name can either be a variable or a hard-coded (quoted) string, |
|---|
| 867 |
in either single or double quotes. |
|---|
| 868 |
|
|---|
| 869 |
This example includes the contents of the template ``"foo/bar.html"``:: |
|---|
| 870 |
|
|---|
| 871 |
{% include "foo/bar.html" %} |
|---|
| 872 |
|
|---|
| 873 |
This example includes the contents of the template whose name is contained in |
|---|
| 874 |
the variable ``template_name``:: |
|---|
| 875 |
|
|---|
| 876 |
{% include template_name %} |
|---|
| 877 |
|
|---|
| 878 |
An included template is rendered with the context of the template that's |
|---|
| 879 |
including it. This example produces the output ``"Hello, John"``: |
|---|
| 880 |
|
|---|
| 881 |
* Context: variable ``person`` is set to ``"john"``. |
|---|
| 882 |
* Template:: |
|---|
| 883 |
|
|---|
| 884 |
{% include "name_snippet.html" %} |
|---|
| 885 |
|
|---|
| 886 |
* The ``name_snippet.html`` template:: |
|---|
| 887 |
|
|---|
| 888 |
Hello, {{ person }} |
|---|
| 889 |
|
|---|
| 890 |
See also: ``{% ssi %}``. |
|---|
| 891 |
|
|---|
| 892 |
load |
|---|
| 893 |
~~~~ |
|---|
| 894 |
|
|---|
| 895 |
Load a custom template tag set. |
|---|
| 896 |
|
|---|
| 897 |
See `Custom tag and filter libraries`_ for more information. |
|---|
| 898 |
|
|---|
| 899 |
now |
|---|
| 900 |
~~~ |
|---|
| 901 |
|
|---|
| 902 |
Display the date, formatted according to the given string. |
|---|
| 903 |
|
|---|
| 904 |
Uses the same format as PHP's ``date()`` function (http://php.net/date) |
|---|
| 905 |
with some custom extensions. |
|---|
| 906 |
|
|---|
| 907 |
Available format strings: |
|---|
| 908 |
|
|---|
| 909 |
================ ======================================== ===================== |
|---|
| 910 |
Format character Description Example output |
|---|
| 911 |
================ ======================================== ===================== |
|---|
| 912 |
a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'`` |
|---|
| 913 |
this is slightly different than PHP's |
|---|
| 914 |
output, because this includes periods |
|---|
| 915 |
to match Associated Press style.) |
|---|
| 916 |
A ``'AM'`` or ``'PM'``. ``'AM'`` |
|---|
| 917 |
b Month, textual, 3 letters, lowercase. ``'jan'`` |
|---|
| 918 |
B Not implemented. |
|---|
| 919 |
d Day of the month, 2 digits with ``'01'`` to ``'31'`` |
|---|
| 920 |
leading zeros. |
|---|
| 921 |
D Day of the week, textual, 3 letters. ``'Fri'`` |
|---|
| 922 |
f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'`` |
|---|
| 923 |
with minutes left off if they're zero. |
|---|
| 924 |
Proprietary extension. |
|---|
| 925 |
F Month, textual, long. ``'January'`` |
|---|
| 926 |
g Hour, 12-hour format without leading ``'1'`` to ``'12'`` |
|---|
| 927 |
zeros. |
|---|
| 928 |
G Hour, 24-hour format without leading ``'0'`` to ``'23'`` |
|---|
| 929 |
zeros. |
|---|
| 930 |
h Hour, 12-hour format. ``'01'`` to ``'12'`` |
|---|
| 931 |
H Hour, 24-hour format. ``'00'`` to ``'23'`` |
|---|
| 932 |
i Minutes. ``'00'`` to ``'59'`` |
|---|
| 933 |
I Not implemented. |
|---|
| 934 |
j Day of the month without leading ``'1'`` to ``'31'`` |
|---|
| 935 |
zeros. |
|---|
| 936 |
l Day of the week, textual, long. ``'Friday'`` |
|---|
| 937 |
L Boolean for whether it's a leap year. ``True`` or ``False`` |
|---|
| 938 |
m Month, 2 digits with leading zeros. ``'01'`` to ``'12'`` |
|---|
| 939 |
M Month, textual, 3 letters. ``'Jan'`` |
|---|
| 940 |
n Month without leading zeros. ``'1'`` to ``'12'`` |
|---|
| 941 |
N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'`` |
|---|
| 942 |
style. Proprietary extension. |
|---|
| 943 |
O Difference to Greenwich time in hours. ``'+0200'`` |
|---|
| 944 |
P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'`` |
|---|
| 945 |
'a.m.'/'p.m.', with minutes left off |
|---|
| 946 |
if they're zero and the special-case |
|---|
| 947 |
strings 'midnight' and 'noon' if |
|---|
| 948 |
appropriate. Proprietary extension. |
|---|
| 949 |
r RFC 2822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'`` |
|---|
| 950 |
s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'`` |
|---|
| 951 |
S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'`` |
|---|
| 952 |
month, 2 characters. |
|---|
| 953 |
t Number of days in the given month. ``28`` to ``31`` |
|---|
| 954 |
T Time zone of this machine. ``'EST'``, ``'MDT'`` |
|---|
| 955 |
U Not implemented. |
|---|
| 956 |
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday) |
|---|
| 957 |
leading zeros. |
|---|
| 958 |
W ISO-8601 week number of year, with ``1``, ``53`` |
|---|
| 959 |
weeks starting on Monday. |
|---|
| 960 |
y Year, 2 digits. ``'99'`` |
|---|
| 961 |
Y Year, 4 digits. ``'1999'`` |
|---|
| 962 |
z Day of the year. ``0`` to ``365`` |
|---|
| 963 |
Z Time zone offset in seconds. The ``-43200`` to ``43200`` |
|---|
| 964 |
offset for timezones west of UTC is |
|---|
| 965 |
always negative, and for those east of |
|---|
| 966 |
UTC is always positive. |
|---|
| 967 |
================ ======================================== ===================== |
|---|
| 968 |
|
|---|
| 969 |
Example:: |
|---|
| 970 |
|
|---|
| 971 |
It is {% now "jS F Y H:i" %} |
|---|
| 972 |
|
|---|
| 973 |
Note that you can backslash-escape a format string if you want to use the |
|---|
| 974 |
"raw" value. In this example, "f" is backslash-escaped, because otherwise |
|---|
| 975 |
"f" is a format string that displays the time. The "o" doesn't need to be |
|---|
| 976 |
escaped, because it's not a format character:: |
|---|
| 977 |
|
|---|
| 978 |
It is the {% now "jS o\f F" %} |
|---|
| 979 |
|
|---|
| 980 |
This would display as "It is the 4th of September". |
|---|
| 981 |
|
|---|
| 982 |
regroup |
|---|
| 983 |
~~~~~~~ |
|---|
| 984 |
|
|---|
| 985 |
Regroup a list of alike objects by a common attribute. |
|---|
| 986 |
|
|---|
| 987 |
This complex tag is best illustrated by use of an example: say that ``people`` |
|---|
| 988 |
is a list of people represented by dictionaries with ``first_name``, |
|---|
| 989 |
``last_name``, and ``gender`` keys:: |
|---|
| 990 |
|
|---|
| 991 |
people = [ |
|---|
| 992 |
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'}, |
|---|
| 993 |
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'}, |
|---|
| 994 |
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'}, |
|---|
| 995 |
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'}, |
|---|
| 996 |
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'}, |
|---|
| 997 |
] |
|---|
| 998 |
|
|---|
| 999 |
...and you'd like to display a hierarchical list that is ordered by gender, |
|---|
| 1000 |
like this: |
|---|
| 1001 |
|
|---|
| 1002 |
* Male: |
|---|
| 1003 |
* George Bush |
|---|
| 1004 |
* Bill Clinton |
|---|
| 1005 |
* Female: |
|---|
| 1006 |
* Margaret Thatcher |
|---|
| 1007 |
* Condoleezza Rice |
|---|
| 1008 |
* Unknown: |
|---|
| 1009 |
* Pat Smith |
|---|
| 1010 |
|
|---|
| 1011 |
You can use the ``{% regroup %}`` tag to group the list of people by gender. |
|---|
| 1012 |
The following snippet of template code would accomplish this:: |
|---|
| 1013 |
|
|---|
| 1014 |
{% regroup people by gender as gender_list %} |
|---|
| 1015 |
|
|---|
| 1016 |
<ul> |
|---|
| 1017 |
{% for gender in gender_list %} |
|---|
| 1018 |
<li>{{ gender.grouper }} |
|---|
| 1019 |
<ul> |
|---|
| 1020 |
{% for item in gender.list %} |
|---|
| 1021 |
<li>{{ item.first_name }} {{ item.last_name }}</li> |
|---|
| 1022 |
{% endfor %} |
|---|
| 1023 |
</ul> |
|---|
| 1024 |
</li> |
|---|
| 1025 |
{% endfor %} |
|---|
| 1026 |
</ul> |
|---|
| 1027 |
|
|---|
| 1028 |
Let's walk through this example. ``{% regroup %}`` takes three arguments: the |
|---|
| 1029 |
list you want to regroup, the attribute to group by, and the name of the |
|---|
| 1030 |
resulting list. Here, we're regrouping the ``people`` list by the ``gender`` |
|---|
| 1031 |
attribute and calling the result ``gender_list``. |
|---|
| 1032 |
|
|---|
| 1033 |
``{% regroup %}`` produces a list (in this case, ``gender_list``) of |
|---|
| 1034 |
**group objects**. Each group object has two attributes: |
|---|
| 1035 |
|
|---|
| 1036 |
* ``grouper`` -- the item that was grouped by (e.g., the string "Male" or |
|---|
| 1037 |
"Female"). |
|---|
| 1038 |
* ``list`` -- a list of all items in this group (e.g., a list of all people |
|---|
| 1039 |
with gender='Male'). |
|---|
| 1040 |
|
|---|
| 1041 |
Note that ``{% regroup %}`` does not order its input! Our example relies on |
|---|
| 1042 |
the fact that the ``people`` list was ordered by ``gender`` in the first place. |
|---|
| 1043 |
If the ``people`` list did *not* order its members by ``gender``, the regrouping |
|---|
| 1044 |
would naively display more than one group for a single gender. For example, |
|---|
| 1045 |
say the ``people`` list was set to this (note that the males are not grouped |
|---|
| 1046 |
together):: |
|---|
| 1047 |
|
|---|
| 1048 |
people = [ |
|---|
| 1049 |
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'}, |
|---|
| 1050 |
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'}, |
|---|
| 1051 |
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'}, |
|---|
| 1052 |
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'}, |
|---|
| 1053 |
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'}, |
|---|
| 1054 |
] |
|---|
| 1055 |
|
|---|
| 1056 |
With this input for ``people``, the example ``{% regroup %}`` template code |
|---|
| 1057 |
above would result in the following output: |
|---|
| 1058 |
|
|---|
| 1059 |
* Male: |
|---|
| 1060 |
* Bill Clinton |
|---|
| 1061 |
* Unknown: |
|---|
| 1062 |
* Pat Smith |
|---|
| 1063 |
* Female: |
|---|
| 1064 |
* Margaret Thatcher |
|---|
| 1065 |
* Male: |
|---|
| 1066 |
* George Bush |
|---|
| 1067 |
* Female: |
|---|
| 1068 |
* Condoleezza Rice |
|---|
| 1069 |
|
|---|
| 1070 |
The easiest solution to this gotcha is to make sure in your view code that the |
|---|
| 1071 |
data is ordered according to how you want to display it. |
|---|
| 1072 |
|
|---|
| 1073 |
Another solution is to sort the data in the template using the ``dictsort`` |
|---|
| 1074 |
filter, if your data is in a list of dictionaries:: |
|---|
| 1075 |
|
|---|
| 1076 |
{% regroup people|dictsort:"gender" by gender as gender_list %} |
|---|
| 1077 |
|
|---|
| 1078 |
spaceless |
|---|
| 1079 |
~~~~~~~~~ |
|---|
| 1080 |
|
|---|
| 1081 |
Removes whitespace between HTML tags. This includes tab |
|---|
| 1082 |
characters and newlines. |
|---|
| 1083 |
|
|---|
| 1084 |
Example usage:: |
|---|
| 1085 |
|
|---|
| 1086 |
{% spaceless %} |
|---|
| 1087 |
<p> |
|---|
| 1088 |
<a href="foo/">Foo</a> |
|---|
| 1089 |
</p> |
|---|
| 1090 |
{% endspaceless %} |
|---|
| 1091 |
|
|---|
| 1092 |
This example would return this HTML:: |
|---|
| 1093 |
|
|---|
| 1094 |
<p><a href="foo/">Foo</a></p> |
|---|
| 1095 |
|
|---|
| 1096 |
Only space between *tags* is removed -- not space between tags and text. In |
|---|
| 1097 |
this example, the space around ``Hello`` won't be stripped:: |
|---|
| 1098 |
|
|---|
| 1099 |
{% spaceless %} |
|---|
| 1100 |
<strong> |
|---|
| 1101 |
Hello |
|---|
| 1102 |
</strong> |
|---|
| 1103 |
{% endspaceless %} |
|---|
| 1104 |
|
|---|
| 1105 |
ssi |
|---|
| 1106 |
~~~ |
|---|
| 1107 |
|
|---|
| 1108 |
Output the contents of a given file into the page. |
|---|
| 1109 |
|
|---|
| 1110 |
Like a simple "include" tag, ``{% ssi %}`` includes the contents of another |
|---|
| 1111 |
file -- which must be specified using an absolute path -- in the current |
|---|
| 1112 |
page:: |
|---|
| 1113 |
|
|---|