Django

Code

root/django/trunk/docs/templates_python.txt

Revision 7294, 58.6 kB (checked in by mtredinnick, 2 months ago)

Added "svn:eol-style native" to every text file in the tree (*.txt, *.html,
*.py, *.xml and AUTHORS, etc). Added "svn:ignore *.pyc" to some directories in
tests/regressiontests/ that were previously missing it.

Fixed #6545, #6801.

  • Property svn:eol-style set to native
Line 
1 ====================================================
2 The Django template language: For Python programmers
3 ====================================================
4
5 This document explains the Django template system from a technical
6 perspective -- how it works and how to extend it. If you're just looking for
7 reference on the language syntax, see
8 `The Django template language: For template authors`_.
9
10 If you're looking to use the Django template system as part of another
11 application -- i.e., without the rest of the framework -- make sure to read
12 the `configuration`_ section later in this document.
13
14 .. _`The Django template language: For template authors`: ../templates/
15
16 Basics
17 ======
18
19 A **template** is a text document, or a normal Python string, that is marked-up
20 using the Django template language. A template can contain **block tags** or
21 **variables**.
22
23 A **block tag** is a symbol within a template that does something.
24
25 This definition is deliberately vague. For example, a block tag can output
26 content, serve as a control structure (an "if" statement or "for" loop), grab
27 content from a database or enable access to other template tags.
28
29 Block tags are surrounded by ``"{%"`` and ``"%}"``.
30
31 Example template with block tags::
32
33     {% if is_logged_in %}Thanks for logging in!{% else %}Please log in.{% endif %}
34
35 A **variable** is a symbol within a template that outputs a value.
36
37 Variable tags are surrounded by ``"{{"`` and ``"}}"``.
38
39 Example template with variables::
40
41     My first name is {{ first_name }}. My last name is {{ last_name }}.
42
43 A **context** is a "variable name" -> "variable value" mapping that is passed
44 to a template.
45
46 A template **renders** a context by replacing the variable "holes" with values
47 from the context and executing all block tags.
48
49 Using the template system
50 =========================
51
52 Using the template system in Python is a two-step process:
53
54     * First, you compile the raw template code into a ``Template`` object.
55     * Then, you call the ``render()`` method of the ``Template`` object with a
56       given context.
57
58 Compiling a string
59 ------------------
60
61 The easiest way to create a ``Template`` object is by instantiating it
62 directly. The class lives at ``django.template.Template``. The constructor
63 takes one argument -- the raw template code::
64
65     >>> from django.template import Template
66     >>> t = Template("My name is {{ my_name }}.")
67     >>> print t
68     <django.template.Template instance>
69
70 .. admonition:: Behind the scenes
71
72     The system only parses your raw template code once -- when you create the
73     ``Template`` object. From then on, it's stored internally as a "node"
74     structure for performance.
75
76     Even the parsing itself is quite fast. Most of the parsing happens via a
77     single call to a single, short, regular expression.
78
79 Rendering a context
80 -------------------
81
82 Once you have a compiled ``Template`` object, you can render a context -- or
83 multiple contexts -- with it. The ``Context`` class lives at
84 ``django.template.Context``, and the constructor takes one (optional)
85 argument: a dictionary mapping variable names to variable values. Call the
86 ``Template`` object's ``render()`` method with the context to "fill" the
87 template::
88
89     >>> from django.template import Context, Template
90     >>> t = Template("My name is {{ my_name }}.")
91
92     >>> c = Context({"my_name": "Adrian"})
93     >>> t.render(c)
94     "My name is Adrian."
95
96     >>> c = Context({"my_name": "Dolores"})
97     >>> t.render(c)
98     "My name is Dolores."
99
100 Variable names must consist of any letter (A-Z), any digit (0-9), an underscore
101 or a dot.
102
103 Dots have a special meaning in template rendering. A dot in a variable name
104 signifies **lookup**. Specifically, when the template system encounters a dot
105 in a variable name, it tries the following lookups, in this order:
106
107     * Dictionary lookup. Example: ``foo["bar"]``
108     * Attribute lookup. Example: ``foo.bar``
109     * Method call. Example: ``foo.bar()``
110     * List-index lookup. Example: ``foo[bar]``
111
112 The template system uses the first lookup type that works. It's short-circuit
113 logic.
114
115 Here are a few examples::
116
117     >>> from django.template import Context, Template
118     >>> t = Template("My name is {{ person.first_name }}.")
119     >>> d = {"person": {"first_name": "Joe", "last_name": "Johnson"}}
120     >>> t.render(Context(d))
121     "My name is Joe."
122
123     >>> class PersonClass: pass
124     >>> p = PersonClass()
125     >>> p.first_name = "Ron"
126     >>> p.last_name = "Nasty"
127     >>> t.render(Context({"person": p}))
128     "My name is Ron."
129
130     >>> class PersonClass2:
131     ...     def first_name(self):
132     ...         return "Samantha"
133     >>> p = PersonClass2()
134     >>> t.render(Context({"person": p}))
135     "My name is Samantha."
136
137     >>> t = Template("The first stooge in the list is {{ stooges.0 }}.")
138     >>> c = Context({"stooges": ["Larry", "Curly", "Moe"]})
139     >>> t.render(c)
140     "The first stooge in the list is Larry."
141
142 Method lookups are slightly more complex than the other lookup types. Here are
143 some things to keep in mind:
144
145     * If, during the method lookup, a method raises an exception, the exception
146       will be propagated, unless the exception has an attribute
147       ``silent_variable_failure`` whose value is ``True``. If the exception
148       *does* have a ``silent_variable_failure`` attribute, the variable will
149       render as an empty string. Example::
150
151         >>> t = Template("My name is {{ person.first_name }}.")
152         >>> class PersonClass3:
153         ...     def first_name(self):
154         ...         raise AssertionError, "foo"
155         >>> p = PersonClass3()
156         >>> t.render(Context({"person": p}))
157         Traceback (most recent call last):
158         ...
159         AssertionError: foo
160
161         >>> class SilentAssertionError(Exception):
162         ...     silent_variable_failure = True
163         >>> class PersonClass4:
164         ...     def first_name(self):
165         ...         raise SilentAssertionError
166         >>> p = PersonClass4()
167         >>> t.render(Context({"person": p}))
168         "My name is ."
169
170       Note that ``django.core.exceptions.ObjectDoesNotExist``, which is the
171       base class for all Django database API ``DoesNotExist`` exceptions, has
172       ``silent_variable_failure = True``. So if you're using Django templates
173       with Django model objects, any ``DoesNotExist`` exception will fail
174       silently.
175
176     * A method call will only work if the method has no required arguments.
177       Otherwise, the system will move to the next lookup type (list-index
178       lookup).
179
180     * Obviously, some methods have side effects, and it'd be either foolish or
181       a security hole to allow the template system to access them.
182
183       A good example is the ``delete()`` method on each Django model object.
184       The template system shouldn't be allowed to do something like this::
185
186         I will now delete this valuable data. {{ data.delete }}
187
188       To prevent this, set a function attribute ``alters_data`` on the method.
189       The template system won't execute a method if the method has
190       ``alters_data=True`` set. The dynamically-generated ``delete()`` and
191       ``save()`` methods on Django model objects get ``alters_data=True``
192       automatically. Example::
193
194         def sensitive_function(self):
195             self.database_record.delete()
196         sensitive_function.alters_data = True
197
198 How invalid variables are handled
199 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
201 Generally, if a variable doesn't exist, the template system inserts the
202 value of the ``TEMPLATE_STRING_IF_INVALID`` setting, which is set to ``''``
203 (the empty string) by default.
204
205 Filters that are applied to an invalid variable will only be applied if
206 ``TEMPLATE_STRING_IF_INVALID`` is set to ``''`` (the empty string). If
207 ``TEMPLATE_STRING_IF_INVALID`` is set to any other value, variable
208 filters will be ignored.
209
210 This behavior is slightly different for the ``if``, ``for`` and ``regroup``
211 template tags. If an invalid variable is provided to one of these template
212 tags, the variable will be interpreted as ``None``. Filters are always
213 applied to invalid variables within these template tags.
214
215 If ``TEMPLATE_STRING_IF_INVALID`` contains a ``'%s'``, the format marker will
216 be replaced with the name of the invalid variable.
217
218 .. admonition:: For debug purposes only!
219
220     While ``TEMPLATE_STRING_IF_INVALID`` can be a useful debugging tool,
221     it is a bad idea to turn it on as a 'development default'.
222
223     Many templates, including those in the Admin site, rely upon the
224     silence of the template system when a non-existent variable is
225     encountered. If you assign a value other than ``''`` to
226     ``TEMPLATE_STRING_IF_INVALID``, you will experience rendering
227     problems with these templates and sites.
228
229     Generally, ``TEMPLATE_STRING_IF_INVALID`` should only be enabled
230     in order to debug a specific template problem, then cleared
231     once debugging is complete.
232
233 Playing with Context objects
234 ----------------------------
235
236 Most of the time, you'll instantiate ``Context`` objects by passing in a
237 fully-populated dictionary to ``Context()``. But you can add and delete items
238 from a ``Context`` object once it's been instantiated, too, using standard
239 dictionary syntax::
240
241     >>> c = Context({"foo": "bar"})
242     >>> c['foo']
243     'bar'
244     >>> del c['foo']
245     >>> c['foo']
246     ''
247     >>> c['newvariable'] = 'hello'
248     >>> c['newvariable']
249     'hello'
250
251 A ``Context`` object is a stack. That is, you can ``push()`` and ``pop()`` it.
252 If you ``pop()`` too much, it'll raise
253 ``django.template.ContextPopException``::
254
255     >>> c = Context()
256     >>> c['foo'] = 'first level'
257     >>> c.push()
258     >>> c['foo'] = 'second level'
259     >>> c['foo']
260     'second level'
261     >>> c.pop()
262     >>> c['foo']
263     'first level'
264     >>> c['foo'] = 'overwritten'
265     >>> c['foo']
266     'overwritten'
267     >>> c.pop()
268     Traceback (most recent call last):
269     ...
270     django.template.ContextPopException
271
272 Using a ``Context`` as a stack comes in handy in some custom template tags, as
273 you'll see below.
274
275 Subclassing Context: RequestContext
276 -----------------------------------
277
278 Django comes with a special ``Context`` class,
279 ``django.template.RequestContext``, that acts slightly differently than
280 the normal ``django.template.Context``. The first difference is that it takes
281 an `HttpRequest object`_ as its first argument. For example::
282
283     c = RequestContext(request, {
284         'foo': 'bar',
285     }
286
287 The second difference is that it automatically populates the context with a few
288 variables, according to your `TEMPLATE_CONTEXT_PROCESSORS setting`_.
289
290 The ``TEMPLATE_CONTEXT_PROCESSORS`` setting is a tuple of callables -- called
291 **context processors** -- that take a request object as their argument and
292 return a dictionary of items to be merged into the context. By default,
293 ``TEMPLATE_CONTEXT_PROCESSORS`` is set to::
294
295     ("django.core.context_processors.auth",
296     "django.core.context_processors.debug",
297     "django.core.context_processors.i18n",
298     "django.core.context_processors.media")
299
300 Each processor is applied in order. That means, if one processor adds a
301 variable to the context and a second processor adds a variable with the same
302 name, the second will override the first. The default processors are explained
303 below.
304
305 Also, you can give ``RequestContext`` a list of additional processors, using the
306 optional, third positional argument, ``processors``. In this example, the
307 ``RequestContext`` instance gets a ``ip_address`` variable::
308
309     def ip_address_processor(request):
310         return {'ip_address': request.META['REMOTE_ADDR']}
311
312     def some_view(request):
313         # ...
314         c = RequestContext(request, {
315             'foo': 'bar',
316         }, [ip_address_processor])
317         return t.render(c)
318
319 .. note::
320     If you're using Django's ``render_to_response()`` shortcut to populate a
321     template with the contents of a dictionary, your template will be passed a
322     ``Context`` instance by default (not a ``RequestContext``). To use a
323     ``RequestContext`` in your template rendering, pass an optional third
324     argument to ``render_to_response()``: a ``RequestContext``
325     instance. Your code might look like this::
326
327         def some_view(request):
328             # ...
329             return render_to_response('my_template.html',
330                                       my_data_dictionary,
331                                       context_instance=RequestContext(request))
332
333 Here's what each of the default processors does:
334
335 .. _HttpRequest object: ../request_response/#httprequest-objects
336 .. _TEMPLATE_CONTEXT_PROCESSORS setting: ../settings/#template-context-processors
337
338 django.core.context_processors.auth
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340
341 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every
342 ``RequestContext`` will contain these three variables:
343
344     * ``user`` -- An ``auth.User`` instance representing the currently
345       logged-in user (or an ``AnonymousUser`` instance, if the client isn't
346       logged in). See the `user authentication docs`_.
347
348     * ``messages`` -- A list of messages (as strings) for the currently
349       logged-in user. Behind the scenes, this calls
350       ``request.user.get_and_delete_messages()`` for every request. That method
351       collects the user's messages and deletes them from the database.
352
353       Note that messages are set with ``user.message_set.create``. See the
354       `message docs`_ for more.
355
356     * ``perms`` -- An instance of
357       ``django.core.context_processors.PermWrapper``, representing the
358       permissions that the currently logged-in user has. See the `permissions
359       docs`_.
360
361 .. _user authentication docs: ../authentication/#users
362 .. _message docs: ../authentication/#messages
363 .. _permissions docs: ../authentication/#permissions
364
365 django.core.context_processors.debug
366 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367
368 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every
369 ``RequestContext`` will contain these two variables -- but only if your
370 ``DEBUG`` setting is set to ``True`` and the request's IP address
371 (``request.META['REMOTE_ADDR']``) is in the ``INTERNAL_IPS`` setting:
372
373     * ``debug`` -- ``True``. You can use this in templates to test whether
374       you're in ``DEBUG`` mode.
375     * ``sql_queries`` -- A list of ``{'sql': ..., 'time': ...}`` dictionaries,
376       representing every SQL query that has happened so far during the request
377       and how long it took. The list is in order by query.
378
379 django.core.context_processors.i18n
380 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
381
382 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every
383 ``RequestContext`` will contain these two variables:
384
385     * ``LANGUAGES`` -- The value of the `LANGUAGES setting`_.
386     * ``LANGUAGE_CODE`` -- ``request.LANGUAGE_CODE``, if it exists. Otherwise,
387       the value of the `LANGUAGE_CODE setting`_.
388
389 See the `internationalization docs`_ for more.
390
391 .. _LANGUAGES setting: ../settings/#languages
392 .. _LANGUAGE_CODE setting: ../settings/#language-code
393 .. _internationalization docs: ../i18n/
394
395 django.core.context_processors.media
396 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
397
398 **New in Django development version**
399
400 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every
401 ``RequestContext`` will contain a variable ``MEDIA_URL``, providing the
402 value of the `MEDIA_URL setting`_.
403
404 .. _MEDIA_URL setting: ../settings/#media-url
405
406 django.core.context_processors.request
407 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
408
409 If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every
410 ``RequestContext`` will contain a variable ``request``, which is the current
411 `HttpRequest object`_. Note that this processor is not enabled by default;
412 you'll have to activate it.
413
414 Writing your own context processors
415 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
416
417 A context processor has a very simple interface: It's just a Python function
418 that takes one argument, an ``HttpRequest`` object, and returns a dictionary
419 that gets added to the template context. Each context processor *must* return
420 a dictionary.
421
422 Custom context processors can live anywhere in your code base. All Django cares
423 about is that your custom context processors are pointed-to by your
424 ``TEMPLATE_CONTEXT_PROCESSORS`` setting.
425
426 Loading templates
427 -----------------
428
429 Generally, you'll store templates in files on your filesystem rather than using
430 the low-level ``Template`` API yourself. Save templates in a directory
431 specified as a **template directory**.
432
433 Django searches for template directories in a number of places, depending on
434 your template-loader settings (see "Loader types" below), but the most basic
435 way of specifying template directories is by using the ``TEMPLATE_DIRS``
436 setting.
437
438 The TEMPLATE_DIRS setting
439 ~~~~~~~~~~~~~~~~~~~~~~~~~
440
441 Tell Django what your template directories are by using the ``TEMPLATE_DIRS``
442 setting in your settings file. This should be set to a list or tuple of strings
443 that contain full paths to your template directory(ies). Example::
444
445     TEMPLATE_DIRS = (
446         "/home/html/templates/lawrence.com",
447         "/home/html/templates/default",
448     )
449
450 Your templates can go anywhere you want, as long as the directories and
451 templates are readable by the Web server. They can have any extension you want,
452 such as ``.html`` or ``.txt``, or they can have no extension at all.
453
454 Note that these paths should use Unix-style forward slashes, even on Windows.
455
456 The Python API
457 ~~~~~~~~~~~~~~
458
459 Django has two ways to load templates from files:
460
461 ``django.template.loader.get_template(template_name)``
462     ``get_template`` returns the compiled template (a ``Template`` object) for
463     the template with the given name. If the template doesn't exist, it raises
464     ``django.template.TemplateDoesNotExist``.
465
466 ``django.template.loader.select_template(template_name_list)``
467     ``select_template`` is just like ``get_template``, except it takes a list
468     of template names. Of the list, it returns the first template that exists.
469
470 For example, if you call ``get_template('story_detail.html')`` and have the
471 above ``TEMPLATE_DIRS`` setting, here are the files Django will look for, in
472 order:
473
474     * ``/home/html/templates/lawrence.com/story_detail.html``
475     * ``/home/html/templates/default/story_detail.html``
476
477 If you call ``select_template(['story_253_detail.html', 'story_detail.html'])``,
478 here's what Django will look for:
479
480     * ``/home/html/templates/lawrence.com/story_253_detail.html``
481     * ``/home/html/templates/default/story_253_detail.html``
482     * ``/home/html/templates/lawrence.com/story_detail.html``
483     * ``/home/html/templates/default/story_detail.html``
484
485 When Django finds a template that exists, it stops looking.
486
487 .. admonition:: Tip
488
489     You can use ``select_template()`` for super-flexible "templatability." For
490     example, if you've written a news story and want some stories to have
491     custom templates, use something like
492     ``select_template(['story_%s_detail.html' % story.id, 'story_detail.html'])``.
493     That'll allow you to use a custom template for an individual story, with a
494     fallback template for stories that don't have custom templates.
495
496 Using subdirectories
497 ~~~~~~~~~~~~~~~~~~~~
498
499 It's possible -- and preferable -- to organize templates in subdirectories of
500 the template directory. The convention is to make a subdirectory for each
501 Django app, with subdirectories within those subdirectories as needed.
502
503 Do this for your own sanity. Storing all templates in the root level of a
504 single directory gets messy.
505
506 To load a template that's within a subdirectory, just use a slash, like so::
507
508     get_template('news/story_detail.html')
509
510 Using the same ``TEMPLATE_DIRS`` setting from above, this example
511 ``get_template()`` call will attempt to load the following templates:
512
513     * ``/home/html/templates/lawrence.com/news/story_detail.html``
514     * ``/home/html/templates/default/news/story_detail.html``
515
516 Loader types
517 ~~~~~~~~~~~~
518
519 By default, Django uses a filesystem-based template loader, but Django comes
520 with a few other template loaders, which know how to load templates from other
521 sources.
522
523 These other loaders are disabled by default, but you can activate them by
524 editing your ``TEMPLATE_LOADERS`` setting. ``TEMPLATE_LOADERS`` should be a
525 tuple of strings, where each string represents a template loader. Here are the
526 template loaders that come with Django:
527
528 ``django.template.loaders.filesystem.load_template_source``
529     Loads templates from the filesystem, according to ``TEMPLATE_DIRS``.
530
531 ``django.template.loaders.app_directories.load_template_source``
532     Loads templates from Django apps on the filesystem. For each app in
533     ``INSTALLED_APPS``, the loader looks for a ``templates`` subdirectory. If
534     the directory exists, Django looks for templates in there.
535
536     This means you can store templates with your individual apps. This also
537     makes it easy to distribute Django apps with default templates.
538
539     For example, for this setting::
540
541         INSTALLED_APPS = ('myproject.polls', 'myproject.music')
542
543     ...then ``get_template('foo.html')`` will look for templates in these
544     directories, in this order:
545
546         * ``/path/to/myproject/polls/templates/foo.html``
547         * ``/path/to/myproject/music/templates/foo.html``
548
549     Note that the loader performs an optimization when it is first imported:
550     It caches a list of which ``INSTALLED_APPS`` packages have a ``templates``
551     subdirectory.
552
553 ``django.template.loaders.eggs.load_template_source``
554     Just like ``app_directories`` above, but it loads templates from Python
555     eggs rather than from the filesystem.
556
557 Django uses the template loaders in order according to the ``TEMPLATE_LOADERS``
558 setting. It uses each loader until a loader finds a match.
559
560 The ``render_to_string()`` shortcut
561 ===================================
562
563 To cut down on the repetitive nature of loading and rendering
564 templates, Django provides a shortcut function which largely
565 automates the process: ``render_to_string()`` in
566 ``django.template.loader``, which loads a template, renders it and
567 returns the resulting string::
568
569     from django.template.loader import render_to_string
570     rendered = render_to_string('my_template.html', { 'foo': 'bar' })
571
572 The ``render_to_string`` shortcut takes one required argument --
573 ``template_name``, which should be the name of the template to load
574 and render -- and two optional arguments::
575
576     dictionary
577         A dictionary to be used as variables and values for the
578         template's context. This can also be passed as the second
579         positional argument.
580
581     context_instance
582         An instance of ``Context`` or a subclass (e.g., an instance of
583         ``RequestContext``) to use as the template's context. This can
584         also be passed as the third positional argument.
585
586 See also the `render_to_response()`_ shortcut, which calls
587 ``render_to_string`` and feeds the result into an ``HttpResponse``
588 suitable for returning directly from a view.
589
590 .. _render_to_response(): ../shortcuts/#render-to-response
591
592 Extending the template system
593 =============================
594
595 Although the Django template language comes with several default tags and
596 filters, you might want to write your own. It's easy to do.
597
598 First, create a ``templatetags`` package in the appropriate Django app's
599 package. It should be on the same level as ``models.py``, ``views.py``, etc. For
600 example::
601
602     polls/
603         models.py
604         templatetags/
605         views.py
606
607 Add two files to the ``templatetags`` package: an ``__init__.py`` file and a
608 file that will contain your custom tag/filter definitions. The name of the
609 latter file is the name you'll use to load the tags later. For example, if your
610 custom tags/filters are in a file called ``poll_extras.py``, you'd do the
611 following in a template::
612
613     {% load poll_extras %}
614
615 The ``{% load %}`` tag looks at your ``INSTALLED_APPS`` setting and only allows
616 the loading of template libraries within installed Django apps. This is a
617 security feature: It allows you to host Python code for many template libraries
618 on a single computer without enabling access to all of them for every Django
619 installation.
620
621 If you write a template library that isn't tied to any particular models/views,
622 it's perfectly OK to have a Django app package that only contains a
623 ``templatetags`` package.
624
625 There's no limit on how many modules you put in the ``templatetags`` package.
626 Just keep in mind that a ``{% load %}`` statement will load tags/filters for
627 the given Python module name, not the name of the app.
628
629 Once you've created that Python module, you'll just have to write a bit of
630 Python code, depending on whether you're writing filters or tags.
631
632 To be a valid tag library, the module must contain a module-level variable
633 named ``register`` that is a ``template.Library`` instance, in which all the
634 tags and filters are registered. So, near the top of your module, put the
635 following::
636
637     from django import template
638
639     register = template.Library()
640
641 .. admonition:: Behind the scenes
642
643     For a ton of examples, read the source code for Django's default filters
644     and tags. They're in ``django/template/defaultfilters.py`` and
645     ``django/template/defaulttags.py``, respectively.
646
647 Writing custom template filters
648 -------------------------------
649
650 Custom filters are just Python functions that take one or two arguments:
651
652     * The value of the variable (input) -- not necessarily a string.
653     * The value of the argument -- this can have a default value, or be left
654       out altogether.
655
656 For example, in the filter ``{{ var|foo:"bar" }}``, the filter ``foo`` would be
657 passed the variable ``var`` and the argument ``"bar"``.
658
659 Filter functions should always return something. They shouldn't raise
660 exceptions. They should fail silently. In case of error, they should return
661 either the original input or an empty string -- whichever makes more sense.
662
663 Here's an example filter definition::
664
665     def cut(value, arg):
666         "Removes all values of arg from the given string"
667         return value.replace(arg, '')
668
669 And here's an example of how that filter would be used::
670
671     {{ somevariable|cut:"0" }}
672
673 Most filters don't take arguments. In this case, just leave the argument out of
674 your function. Example::
675
676     def lower(value): # Only one argument.
677         "Converts a string into all lowercase"
678         return value.lower()
679
680 Template filters that expect strings
681 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
682
683 If you're writing a template filter that only expects a string as the first
684 argument, you should use the decorator ``stringfilter``. This will
685 convert an object to its string value before being passed to your function::
686
687     from django.template.defaultfilters import stringfilter
688
689     @stringfilter
690     def lower(value):
691         return value.lower()
692
693 This way, you'll be able to pass, say, an integer to this filter, and it
694 won't cause an ``AttributeError`` (because integers don't have ``lower()``
695 methods).
696
697 Registering custom filters
698 ~~~~~~~~~~~~~~~~~~~~~~~~~~
699
700 Once you've written your filter definition, you need to register it with
701 your ``Library`` instance, to make it available to Django's template language::
702
703     register.filter('cut', cut)
704     register.filter('lower', lower)
705
706 The ``Library.filter()`` method takes two arguments:
707
708     1. The name of the filter -- a string.
709     2. The compilation function -- a Python function (not the name of the
710        function as a string).
711
712 If you're using Python 2.4 or above, you can use ``register.filter()`` as a
713 decorator instead::
714
715     @register.filter(name='cut')
716     @stringfilter
717     def cut(value, arg):
718         return value.replace(arg, '')
719
720     @register.filter
721     @stringfilter
722     def lower(value):
723         return value.lower()
724
725 If you leave off the ``name`` argument, as in the second example above, Django
726 will use the function's name as the filter name.
727
728 Filters and auto-escaping
729 ~~~~~~~~~~~~~~~~~~~~~~~~~
730
731 **New in Django development version**
732
733 When writing a custom filter, give some thought to how the filter will interact
734 with Django's auto-escaping behavior. Note that three types of strings can be
735 passed around inside the template code:
736
737     * **Raw strings** are the native Python ``str`` or ``unicode`` types. On
738       output, they're escaped if auto-escaping is in effect and presented
739       unchanged, otherwise.
740
741     * **Safe strings** are strings that have been marked safe from further
742       escaping at output time. Any necessary escaping has already been done.
743       They're commonly used for output that contains raw HTML that is intended
744       to be interpreted as-is on the client side.
745
746       Internally, these strings are of type ``SafeString`` or ``SafeUnicode``.
747       They share a common base class of ``SafeData``, so you can test
748       for them using code like::
749
750           if isinstance(value, SafeData):
751               # Do something with the "safe" string.
752
753     * **Strings marked as "needing escaping"** are *always* escaped on
754       output, regardless of whether they are in an ``autoescape`` block or not.
755       These strings are only escaped once, however, even if auto-escaping
756       applies.
757
758       Internally, these strings are of type ``EscapeString`` or
759       ``EscapeUnicode``. Generally you don't have to worry about these; they
760       exist for the implementation of the ``escape`` filter.
761
762 Template filter code falls into one of two situations:
763
764     1. Your filter does not introduce any HTML-unsafe characters (``<``, ``>``,
765        ``'``, ``"`` or ``&``) into the result that were not already present. In
766        this case, you can let Django take care of all the auto-escaping
767        handling for you. All you need to do is put the ``is_safe`` attribute on
768        your filter function and set it to ``True``, like so::
769
770            @register.filter
771            def myfilter(value):
772                return value
773            myfilter.is_safe = True
774
775        This attribute tells Django that if a "safe" string is passed into your
776        filter, the result will still be "safe" and if a non-safe string is
777        passed in, Django will automatically escape it, if necessary.
778
779        You can think of this as meaning "this filter is safe -- it doesn't
780        introduce any possibility of unsafe HTML."
781
782        The reason ``is_safe`` is necessary is because there are plenty of
783        normal string operations that will turn a ``SafeData`` object back into
784        a normal ``str`` or ``unicode`` object and, rather than try to catch
785        them all, which would be very difficult, Django repairs the damage after
786        the filter has completed.
787
788        For example, suppose you have a filter that adds the string ``xx`` to the
789        end of any input. Since this introduces no dangerous HTML characters to
790        the result (aside from any that were already present), you should mark
791        your filter with ``is_safe``::
792
793         @register.filter
794         def add_xx(value):
795             return '%sxx' % value
796         add_xx.is_safe = True
797
798        When this filter is used in a template where auto-escaping is enabled,
799        Django will escape the output whenever the input is not already marked as
800        "safe".
801
802        By default, ``is_safe`` defaults to ``False``, and you can omit it from
803        any filters where it isn't required.
804
805        Be careful when deciding if your filter really does leave safe strings
806        as safe. If you're *removing* characters, you might inadvertently leave
807        unbalanced HTML tags or entities in the result. For example, removing a
808        ``>`` from the input might turn ``<a>`` into ``<a``, which would need to
809        be escaped on output to avoid causing problems. Similarly, removing a
810        semicolon (``;``) can turn ``&amp;`` into ``&amp``, which is no longer a
811        valid entity and thus needs further escaping. Most cases won't be nearly
812        this tricky, but keep an eye out for any problems like that when
813        reviewing your code.
814
815     2. Alternatively, your filter code can manually take care of any necessary
816        escaping. This is necessary when you're introducing new HTML markup into
817        the result. You want to mark the output as safe from further
818        escaping so that your HTML markup isn't escaped further, so you'll need
819        to handle the input yourself.
820
821        To mark the output as a safe string, use ``django.utils.safestring.mark_safe()``.
822
823        Be careful, though. You need to do more than just mark the output as
824        safe. You need to ensure it really *is* safe, and what you do depends on
825        whether auto-escaping is in effect. The idea is to write filters than
826        can operate in templates where auto-escaping is either on or off in
827        order to make things easier for your template authors.
828
829        In order for your filter to know the current auto-escaping state, set
830        the ``needs_autoescape`` attribute to ``True`` on your function. (If you
831        don't specify this attribute, it defaults to ``False``). This attribute
832        tells Django that your filter function wants to be passed an extra
833        keyword argument, called ``autoescape``, that is ``True`` if
834        auto-escaping is in effect and ``False`` otherwise.
835
836        For example, let's write a filter that emphasizes the first character of
837        a string::
838
839            from django.utils.html import conditional_escape
840            from django.utils.safestring import mark_safe
841
842            def initial_letter_filter(text, autoescape=None):
843                first, other = text[0], text[1:]
844                if autoescape:
845                    esc = conditional_escape
846                else:
847                    esc = lambda x: x
848                result = '<strong>%s</strong>%s' % (esc(first), esc(other))
849                return mark_safe(result)
850            initial_letter_filter.needs_autoescape = True
851
852        The ``needs_autoescape`` attribute on the filter function and the
853        ``autoescape`` keyword argument mean that our function will know whether
854        automatic escaping is in effect when the filter is called. We use
855        ``autoescape`` to decide whether the input data needs to be passed through
856        ``django.utils.html.conditional_escape`` or not. (In the latter case, we
857        just use the identity function as the "escape" function.) The
858        ``conditional_escape()`` function is like ``escape()`` except it only
859        escapes input that is **not** a ``SafeData`` instance. If a ``SafeData``
860        instance is passed to ``conditional_escape()``, the data is returned
861        unchanged.
862
863        Finally, in the above example, we remember to mark the result as safe
864        so that our HTML is inserted directly into the template without further
865        escaping.
866
867        There's no need to worry about the ``is_safe`` attribute in this case
868        (although including it wouldn't hurt anything). Whenever you manually
869        handle the auto-escaping issues and return a safe string, the
870        ``is_safe`` attribute won't change anything either way.
871
872 Writing custom template tags
873 ----------------------------
874
875 Tags are more complex than filters, because tags can do anything.
876
877 A quick overview
878 ~~~~~~~~~~~~~~~~
879
880 Above, this document explained that the template system works in a two-step
881 process: compiling and rendering. To define a custom template tag, you specify
882 how the compilation works and how the rendering works.
883
884 When Django compiles a template, it splits the raw template text into
885 ''nodes''. Each node is an instance of ``django.template.Node`` and has
886 a ``render()`` method. A compiled template is, simply, a list of ``Node``
887 objects. When you call ``render()`` on a compiled template object, the template
888 calls ``render()`` on each ``Node`` in its node list, with the given context.
889 The results are all concatenated together to form the output of the template.
890
891 Thus, to define a custom template tag, you specify how the raw template tag is
892 converted into a ``Node`` (the compilation function), and what the node's
893 ``render()`` method does.
894
895 Writing the compilation function
896 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
897
898 For each template tag the template parser encounters, it calls a Python
899 function with the tag contents and the parser object itself. This function is
900 responsible for returning a ``Node`` instance based on the contents of the tag.
901
902 For example, let's write a template tag, ``{% current_time %}``, that displays
903 the current date/time, formatted according to a parameter given in the tag, in
904 `strftime syntax`_. It's a good idea to decide the tag syntax before anything
905 else. In our case, let's say the tag should be used like this::
906
907     <p>The time is {% current_time "%Y-%m-%d %I:%M %p" %}.</p>
908
909 .. _`strftime syntax`: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
910
911 The parser for this function should grab the parameter and create a ``Node``
912 object::
913
914     from django import template
915     def do_current_time(parser, token):
916         try:
917             # split_contents() knows not to split quoted strings.
918             tag_name, format_string = token.split_contents()
919         except ValueError:
920             raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
921         if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
922             raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
923         return CurrentTimeNode(format_string[1:-1])
924
925 Notes:
926
927     * ``parser`` is the template parser object. We don't need it in this
928       example.
929
930     * ``token.contents`` is a string of the raw contents of the tag. In our
931       example, it's ``'current_time "%Y-%m-%d %I:%M %p"'``.
932
933     * The ``token.split_contents()`` method separates the arguments on spaces
934       while keeping quoted strings together. The more straightforward
935       ``token.contents.split()`` wouldn't be as robust, as it would naively
936       split on *all* spaces, including those within quoted strings. It's a good
937       idea to always use ``token.split_contents()``.
938
939     * This function is responsible for raising
940       ``django.template.TemplateSyntaxError``, with helpful messages, for
941       any syntax error.
942
943     * The ``TemplateSyntaxError`` exceptions use the ``tag_name`` variable.
944       Don't hard-code the tag's name in your error messages, because that
945       couples the tag's name to your function. ``token.contents.split()[0]``
946       will ''always'' be the name of your tag -- even when the tag has no
947       arguments.
948
949     * The function returns a ``CurrentTimeNode`` with everything the node needs
950       to know about this tag. In this case, it just passes the argument --
951       ``"%Y-%m-%d %I:%M %p"``. The leading and trailing quotes from the
952       template tag are removed in ``format_string[1:-1]``.
953
954     * The parsing is very low-level. The Django developers have experimented
955       with writing small frameworks on top of this parsing system, using
956       techniques such as EBNF grammars, but those experiments made the template
957       engine too slow. It's low-level because that's fastest.
958
959 Writing the renderer
960 ~~~~~~~~~~~~~~~~~~~~
961
962 The second step in writing custom tags is to define a ``Node`` subclass that
963 has a ``render()`` method.
964
965 Continuing the above example, we need to define ``CurrentTimeNode``::
966
967     from django import template
968     import datetime
969     class CurrentTimeNode(template.Node):
970         def __init__(self, format_string):
971             self.format_string = format_string
972         def render(self, context):
973             return datetime.datetime.now().strftime(self.format_string)
974
975 Notes:
976
977     * ``__init__()`` gets the ``format_string`` from ``do_current_time()``.
978       Always pass any options/parameters/arguments to a ``Node`` via its
979       ``__init__()``.
980
981     * The ``render()`` method is where the work actually happens.
982
983     * ``render()`` should never raise ``TemplateSyntaxError`` or any other
984       exception. It should fail silently, just as template filters should.
985
986 Ultimately, this decoupling of compilation and rendering results in an
987 efficient template system, because a template can render multiple contexts
988 without having to be parsed multiple times.
989
990 Auto-escaping considerations
991 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
992
993 **New in Django development version**
994
995 The output from template tags is **not** automatically run through the
996 auto-escaping filters. However, there are still a couple of things you should
997 keep in mind when writing a template tag.
998
999 If the ``render()`` function of your template stores the result in a context
1000 variable (rather than returning the result in a string), it should take care
1001 to call ``mark_safe()`` if appropriate. When the variable is ultimately
1002 rendered, it will be affected by the auto-escape setting in effect at the
1003 time, so content that should be safe from further escaping needs to be marked
1004 as such.
1005
1006 Also, if your template tag creates a new context for performing some
1007 sub-rendering, set the auto-escape attribute to the current context's value.
1008 The ``__init__`` method for the ``Context`` class takes a parameter called
1009 ``autoescape`` that you can use for this purpose. For example::
1010
1011     def render(self, context):
1012         # ...
1013         new_context = Context({'var': obj}, autoescape=context.autoescape)
1014         # ... Do something with new_context ...
1015
1016 This is not a very common situation, but it's useful if you're rendering a
1017 template yourself. For example::
1018
1019     def render(self, context):
1020         t = template.loader.get_template('small_fragment.html')
1021         return t.render(Context({'var': obj}, autoescape=context.autoescape))
1022
1023 If we had neglected to pass in the current ``context.autoescape`` value to our
1024 new ``Context`` in this example, the results would have *always* been
1025 automatically escaped, which may not be the desired behavior if the template
1026 tag is used inside a ``{% autoescape off %}`` block.
1027
1028 Registering the tag
1029 ~~~~~~~~~~~~~~~~~~~
1030
1031 Finally, register the tag with your module's ``Library`` instance, as explained
1032 in "Writing custom template filters" above. Example::
1033
1034     register.tag('current_time', do_current_time)
1035
1036 The ``tag()`` method takes two arguments:
1037
1038     1. The name of the template tag -- a string. If this is left out, the
1039        name of the compilation function will be used.
1040     2. The compilation function -- a Python function (not the name of the
1041        function as a string).
1042
1043 As with filter registration, it is also possible to use this as a decorator, in
1044 Python 2.4 and above::
1045
1046     @register.tag(name="current_time")
1047     def do_current_time(parser, token):
1048         # ...
1049
1050     @register.tag
1051     def shout(parser, token):
1052         # ...
1053
1054 If you leave off the ``name`` argument, as in the second example above, Django
1055 will use the function's name as the tag name.
1056
1057 Passing template variables to the tag
1058 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1059
1060 Although you can pass any number of arguments to a template tag using
1061 ``token.split_contents()``, the arguments are all unpacked as
1062 string literals. A little more work is required in order to dynamic content (a
1063 template variable) to a template tag as an argument.
1064
1065 While the previous examples have formatted the current time into a string and
1066 returned the string, suppose you wanted to pass in a ``DateTimeField`` from an
1067 object and have the template tag format that date-time::
1068
1069     <p>This post was last updated at {% format_time blog_entry.date_updated "%Y-%m-%d %I:%M %p" %}.</p>
1070
1071 Initially, ``token.split_contents()`` will return three values:
1072
1073     1. The tag name ``format_time``.
1074     2. The string "blog_entry.date_updated" (without the surrounding quotes).
1075     3. The formatting string "%Y-%m-%d %I:%M %p". The return value from
1076        ``split_contents()`` will include the leading and trailing quotes for
1077        string literals like this.
1078
1079 Now your tag should begin to look like this::
1080
1081     from django import template
1082     def do_format_time(parser, token):
1083         try:
1084             # split_contents() knows not to split quoted strings.
1085             tag_name, date_to_be_formatted, format_string = token.split_contents()
1086         except ValueError:
1087             raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0]
1088         if not (format_string[0] == format_string[-1] and format_string[0] in ('"', "'")):
1089             raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name
1090         return FormatTimeNode(date_to_be_formatted, format_string[1:-1])
1091
1092 You also have to change the renderer to retrieve the actual contents of the
1093 ``date_updated`` property of the ``blog_entry`` object.  This can be
1094 accomplished by using the ``resolve_variable()`` function in
1095 ``django.template``. You pass