2008-08-24 06:25:40 +08:00
|
|
|
==================================
|
|
|
|
Built-in template tags and filters
|
|
|
|
==================================
|
2007-11-30 13:18:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
This document describes Django's built-in template tags and filters. It is
|
|
|
|
recommended that you use the :ref:`automatic documentation
|
|
|
|
<template-built-in-reference>`, if available, as this will also include
|
|
|
|
documentation for any custom tags or filters installed.
|
2007-10-19 13:07:47 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _ref-templates-builtins-tags:
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
Built-in tag reference
|
|
|
|
----------------------
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
.. highlightlang:: html+django
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: autoescape
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
autoescape
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2007-12-04 14:01:00 +08:00
|
|
|
Control the current auto-escaping behavior. This tag takes either ``on`` or
|
2007-11-14 20:58:53 +08:00
|
|
|
``off`` as an argument and that determines whether auto-escaping is in effect
|
2010-09-08 04:49:32 +08:00
|
|
|
inside the block. The block is closed with an ``endautoescape`` ending tag.
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
When auto-escaping is in effect, all variable content has HTML escaping applied
|
|
|
|
to it before placing the result into the output (but after any filters have
|
|
|
|
been applied). This is equivalent to manually applying the ``escape`` filter
|
2007-12-04 14:01:00 +08:00
|
|
|
to each variable.
|
2007-11-14 20:58:53 +08:00
|
|
|
|
2007-12-04 14:01:00 +08:00
|
|
|
The only exceptions are variables that are already marked as "safe" from
|
|
|
|
escaping, either by the code that populated the variable, or because it has had
|
2007-11-14 20:58:53 +08:00
|
|
|
the ``safe`` or ``escape`` filters applied.
|
|
|
|
|
2010-09-08 04:49:32 +08:00
|
|
|
Sample usage::
|
|
|
|
|
|
|
|
{% autoescape on %}
|
|
|
|
{{ body }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: block
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
block
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Define a block that can be overridden by child templates. See
|
2008-08-24 06:25:40 +08:00
|
|
|
:ref:`Template inheritance <template-inheritance>` for more information.
|
|
|
|
|
|
|
|
.. templatetag:: comment
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
comment
|
|
|
|
~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Ignore everything between ``{% comment %}`` and ``{% endcomment %}``
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2009-11-23 21:44:24 +08:00
|
|
|
.. templatetag:: csrf_token
|
2008-08-24 06:25:40 +08:00
|
|
|
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
csrf_token
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2009-10-28 05:52:25 +08:00
|
|
|
.. versionadded:: 1.1.2
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2009-10-28 05:52:25 +08:00
|
|
|
In the Django 1.1.X series, this is a no-op tag that returns an empty string for
|
|
|
|
future compatibility purposes. In Django 1.2 and later, it is used for CSRF
|
2010-08-20 03:27:44 +08:00
|
|
|
protection, as described in the documentation for :doc:`Cross Site Request
|
|
|
|
Forgeries </ref/contrib/csrf>`.
|
Fixed #9977 - CsrfMiddleware gets template tag added, session dependency removed, and turned on by default.
This is a large change to CSRF protection for Django. It includes:
* removing the dependency on the session framework.
* deprecating CsrfResponseMiddleware, and replacing with a core template tag.
* turning on CSRF protection by default by adding CsrfViewMiddleware to
the default value of MIDDLEWARE_CLASSES.
* protecting all contrib apps (whatever is in settings.py)
using a decorator.
For existing users of the CSRF functionality, it should be a seamless update,
but please note that it includes DEPRECATION of features in Django 1.1,
and there are upgrade steps which are detailed in the docs.
Many thanks to 'Glenn' and 'bthomas', who did a lot of the thinking and work
on the patch, and to lots of other people including Simon Willison and
Russell Keith-Magee who refined the ideas.
Details of the rationale for these changes is found here:
http://code.djangoproject.com/wiki/CsrfProtection
As of this commit, the CSRF code is mainly in 'contrib'. The code will be
moved to core in a separate commit, to make the changeset as readable as
possible.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-10-27 07:23:07 +08:00
|
|
|
|
2009-11-23 21:44:24 +08:00
|
|
|
.. templatetag:: cycle
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
cycle
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionchanged:: 1.0
|
|
|
|
Cycle among the given strings or variables each time this tag is encountered.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
Within a loop, cycles among the given strings each time through the
|
2007-09-14 10:49:21 +08:00
|
|
|
loop::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
{% for o in some_list %}
|
2008-10-01 01:45:58 +08:00
|
|
|
<tr class="{% cycle 'row1' 'row2' %}">
|
2005-11-07 07:30:06 +08:00
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
2007-10-20 19:05:15 +08:00
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
You can use variables, too. For example, if you have two template variables,
|
|
|
|
``rowvalue1`` and ``rowvalue2``, you can cycle between their values like this::
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
{% for o in some_list %}
|
|
|
|
<tr class="{% cycle rowvalue1 rowvalue2 %}">
|
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
Yes, you can mix variables and strings::
|
|
|
|
|
|
|
|
{% for o in some_list %}
|
|
|
|
<tr class="{% cycle 'row1' rowvalue2 'row3' %}">
|
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
In some cases you might want to refer to the next value of a cycle from
|
|
|
|
outside of a loop. To do this, just give the ``{% cycle %}`` tag a name, using
|
|
|
|
"as", like this::
|
|
|
|
|
|
|
|
{% cycle 'row1' 'row2' as rowcolors %}
|
|
|
|
|
|
|
|
From then on, you can insert the current value of the cycle wherever you'd like
|
|
|
|
in your template::
|
|
|
|
|
|
|
|
<tr class="{% cycle rowcolors %}">...</tr>
|
|
|
|
<tr class="{% cycle rowcolors %}">...</tr>
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
You can use any number of values in a ``{% cycle %}`` tag, separated by spaces.
|
|
|
|
Values enclosed in single (``'``) or double quotes (``"``) are treated as
|
|
|
|
string literals, while values without quotes are treated as template variables.
|
2007-09-14 10:49:21 +08:00
|
|
|
|
2010-03-23 22:52:51 +08:00
|
|
|
Note that the variables included in the cycle will not be escaped.
|
|
|
|
This is because template tags do not escape their content. Any HTML or
|
|
|
|
Javascript code contained in the printed variable will be rendered
|
|
|
|
as-is, which could potentially lead to security issues.
|
|
|
|
|
|
|
|
If you need to escape the variables in the cycle, you must do so
|
|
|
|
explicitly::
|
2009-07-03 13:41:36 +08:00
|
|
|
|
|
|
|
{% filter force_escape %}
|
|
|
|
{% cycle var1 var2 var3 %}
|
|
|
|
{% endfilter %}
|
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
For backwards compatibility, the ``{% cycle %}`` tag supports the much inferior
|
|
|
|
old syntax from previous Django versions. You shouldn't use this in any new
|
|
|
|
projects, but for the sake of the people who are still using it, here's what it
|
|
|
|
looks like::
|
2007-09-14 10:49:21 +08:00
|
|
|
|
|
|
|
{% cycle row1,row2,row3 %}
|
2007-10-20 19:05:15 +08:00
|
|
|
|
2008-10-01 01:45:58 +08:00
|
|
|
In this syntax, each value gets interpreted as a literal string, and there's no
|
|
|
|
way to specify variable values. Or literal commas. Or spaces. Did we mention
|
|
|
|
you shouldn't use this syntax in any new projects?
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: debug
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
debug
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Output a whole load of debugging information, including the current context and
|
|
|
|
imported modules.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: extends
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
extends
|
|
|
|
~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Signal that this template extends a parent template.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-08-22 02:27:33 +08:00
|
|
|
This tag can be used in two ways:
|
2006-07-28 07:23:55 +08:00
|
|
|
|
|
|
|
* ``{% extends "base.html" %}`` (with quotes) uses the literal value
|
2006-07-28 10:25:46 +08:00
|
|
|
``"base.html"`` as the name of the parent template to extend.
|
2006-07-28 07:23:55 +08:00
|
|
|
|
2006-07-28 10:25:46 +08:00
|
|
|
* ``{% extends variable %}`` uses the value of ``variable``. If the variable
|
|
|
|
evaluates to a string, Django will use that string as the name of the
|
|
|
|
parent template. If the variable evaluates to a ``Template`` object,
|
|
|
|
Django will use that object as the parent template.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
See :ref:`template-inheritance` for more information.
|
|
|
|
|
|
|
|
.. templatetag:: filter
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
filter
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Filter the contents of the variable through variable filters.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Filters can also be piped through each other, and they can have arguments --
|
|
|
|
just like in variable syntax.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Sample usage::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
{% filter force_escape|lower %}
|
2005-11-07 07:30:06 +08:00
|
|
|
This text will be HTML-escaped, and will appear in all lowercase.
|
|
|
|
{% endfilter %}
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: firstof
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
firstof
|
|
|
|
~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2009-07-03 13:41:36 +08:00
|
|
|
Outputs the first variable passed that is not False, without escaping.
|
|
|
|
|
|
|
|
Outputs nothing if all the passed variables are False.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Sample usage::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
{% firstof var1 var2 var3 %}
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
This is equivalent to::
|
|
|
|
|
|
|
|
{% if var1 %}
|
2009-07-03 13:41:36 +08:00
|
|
|
{{ var1|safe }}
|
2005-11-07 07:30:06 +08:00
|
|
|
{% else %}{% if var2 %}
|
2009-07-03 13:41:36 +08:00
|
|
|
{{ var2|safe }}
|
2005-11-07 07:30:06 +08:00
|
|
|
{% else %}{% if var3 %}
|
2009-07-03 13:41:36 +08:00
|
|
|
{{ var3|safe }}
|
2005-11-07 07:30:06 +08:00
|
|
|
{% endif %}{% endif %}{% endif %}
|
|
|
|
|
2007-10-20 23:01:31 +08:00
|
|
|
You can also use a literal string as a fallback value in case all
|
|
|
|
passed variables are False::
|
|
|
|
|
|
|
|
{% firstof var1 var2 var3 "fallback value" %}
|
|
|
|
|
2010-03-23 22:52:51 +08:00
|
|
|
Note that the variables included in the firstof tag will not be
|
|
|
|
escaped. This is because template tags do not escape their content.
|
|
|
|
Any HTML or Javascript code contained in the printed variable will be
|
|
|
|
rendered as-is, which could potentially lead to security issues.
|
|
|
|
|
|
|
|
If you need to escape the variables in the firstof tag, you must do so
|
|
|
|
explicitly::
|
2009-07-03 13:41:36 +08:00
|
|
|
|
|
|
|
{% filter force_escape %}
|
|
|
|
{% firstof var1 var2 var3 "fallback value" %}
|
|
|
|
{% endfilter %}
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: for
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
for
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Loop over each item in an array. For example, to display a list of athletes
|
2007-06-08 19:58:03 +08:00
|
|
|
provided in ``athlete_list``::
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
2007-06-08 19:58:03 +08:00
|
|
|
You can loop over a list in reverse by using ``{% for obj in list reversed %}``.
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
2007-06-08 19:58:03 +08:00
|
|
|
If you need to loop over a list of lists, you can unpack the values
|
2008-07-30 20:37:38 +08:00
|
|
|
in each sub-list into individual variables. For example, if your context
|
|
|
|
contains a list of (x,y) coordinates called ``points``, you could use the
|
|
|
|
following to output the list of points::
|
2007-06-08 19:58:03 +08:00
|
|
|
|
|
|
|
{% for x, y in points %}
|
|
|
|
There is a point at {{ x }},{{ y }}
|
|
|
|
{% endfor %}
|
2007-10-20 19:05:15 +08:00
|
|
|
|
|
|
|
This can also be useful if you need to access the items in a dictionary.
|
2007-06-08 19:58:03 +08:00
|
|
|
For example, if your context contained a dictionary ``data``, the following
|
|
|
|
would display the keys and values of the dictionary::
|
|
|
|
|
|
|
|
{% for key, value in data.items %}
|
|
|
|
{{ key }}: {{ value }}
|
|
|
|
{% endfor %}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
The for loop sets a number of variables available within the loop:
|
|
|
|
|
|
|
|
========================== ================================================
|
|
|
|
Variable Description
|
|
|
|
========================== ================================================
|
|
|
|
``forloop.counter`` The current iteration of the loop (1-indexed)
|
|
|
|
``forloop.counter0`` The current iteration of the loop (0-indexed)
|
|
|
|
``forloop.revcounter`` The number of iterations from the end of the
|
|
|
|
loop (1-indexed)
|
|
|
|
``forloop.revcounter0`` The number of iterations from the end of the
|
|
|
|
loop (0-indexed)
|
|
|
|
``forloop.first`` True if this is the first time through the loop
|
|
|
|
``forloop.last`` True if this is the last time through the loop
|
|
|
|
``forloop.parentloop`` For nested loops, this is the loop "above" the
|
|
|
|
current one
|
|
|
|
========================== ================================================
|
|
|
|
|
2008-11-25 06:01:48 +08:00
|
|
|
for ... empty
|
|
|
|
^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
|
|
|
The ``for`` tag can take an optional ``{% empty %}`` clause that will be
|
|
|
|
displayed if the given array is empty or could not be found::
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% empty %}
|
|
|
|
<li>Sorry, no athlete in this list!</li>
|
|
|
|
{% endfor %}
|
|
|
|
<ul>
|
|
|
|
|
|
|
|
The above is equivalent to -- but shorter, cleaner, and possibly faster
|
|
|
|
than -- the following::
|
|
|
|
|
|
|
|
<ul>
|
2008-12-03 13:50:46 +08:00
|
|
|
{% if athlete_list %}
|
2008-11-25 06:01:48 +08:00
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
{% else %}
|
|
|
|
<li>Sorry, no athletes in this list.</li>
|
|
|
|
{% endif %}
|
|
|
|
</ul>
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: if
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
if
|
|
|
|
~~
|
|
|
|
|
|
|
|
The ``{% if %}`` tag evaluates a variable, and if that variable is "true" (i.e.
|
|
|
|
exists, is not empty, and is not a false boolean value) the contents of the
|
|
|
|
block are output::
|
|
|
|
|
|
|
|
{% if athlete_list %}
|
|
|
|
Number of athletes: {{ athlete_list|length }}
|
|
|
|
{% else %}
|
|
|
|
No athletes.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
In the above, if ``athlete_list`` is not empty, the number of athletes will be
|
|
|
|
displayed by the ``{{ athlete_list|length }}`` variable.
|
|
|
|
|
2006-11-08 07:42:27 +08:00
|
|
|
As you can see, the ``if`` tag can take an optional ``{% else %}`` clause that
|
2005-11-07 07:30:06 +08:00
|
|
|
will be displayed if the test fails.
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
Boolean operators
|
|
|
|
^^^^^^^^^^^^^^^^^
|
|
|
|
|
2006-06-08 11:33:21 +08:00
|
|
|
``if`` tags may use ``and``, ``or`` or ``not`` to test a number of variables or
|
|
|
|
to negate a given variable::
|
|
|
|
|
|
|
|
{% if athlete_list and coach_list %}
|
|
|
|
Both athletes and coaches are available.
|
|
|
|
{% endif %}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
{% if not athlete_list %}
|
|
|
|
There are no athletes.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if athlete_list or coach_list %}
|
|
|
|
There are some athletes or some coaches.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if not athlete_list or coach_list %}
|
|
|
|
There are no athletes or there are some coaches (OK, so
|
|
|
|
writing English translations of boolean logic sounds
|
2006-06-08 11:33:21 +08:00
|
|
|
stupid; it's not our fault).
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if athlete_list and not coach_list %}
|
|
|
|
There are some athletes and absolutely no coaches.
|
2005-11-07 07:30:06 +08:00
|
|
|
{% endif %}
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
.. versionchanged:: 1.2
|
|
|
|
|
|
|
|
Use of both ``and`` and ``or`` clauses within the same tag is allowed, with
|
|
|
|
``and`` having higher precedence than ``or`` e.g.::
|
2006-06-08 11:33:21 +08:00
|
|
|
|
|
|
|
{% if athlete_list and coach_list or cheerleader_list %}
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
will be interpreted like:
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
if (athlete_list and coach_list) or cheerleader_list
|
|
|
|
|
|
|
|
Use of actual brackets in the ``if`` tag is invalid syntax. If you need them to
|
|
|
|
indicate precedence, you should use nested ``if`` tags.
|
|
|
|
|
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
|
|
|
|
|
|
|
``if`` tags may also use the operators ``==``, ``!=``, ``<``, ``>``,
|
|
|
|
``<=``, ``>=`` and ``in`` which work as follows:
|
|
|
|
|
|
|
|
|
|
|
|
``==`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Equality. Example::
|
|
|
|
|
|
|
|
{% if somevar == "x" %}
|
|
|
|
This appears if variable somevar equals the string "x"
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``!=`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Inequality. Example::
|
|
|
|
|
|
|
|
{% if somevar != "x" %}
|
|
|
|
This appears if variable somevar does not equal the string "x",
|
|
|
|
or if somevar is not found in the context
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``<`` operator
|
|
|
|
^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Less than. Example::
|
|
|
|
|
|
|
|
{% if somevar < 100 %}
|
|
|
|
This appears if variable somevar is less than 100.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``>`` operator
|
|
|
|
^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Greater than. Example::
|
|
|
|
|
|
|
|
{% if somevar > 0 %}
|
|
|
|
This appears if variable somevar is greater than 0.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``<=`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Less than or equal to. Example::
|
|
|
|
|
|
|
|
{% if somevar <= 100 %}
|
|
|
|
This appears if variable somevar is less than 100 or equal to 100.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``>=`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Greater than or equal to. Example::
|
|
|
|
|
|
|
|
{% if somevar >= 1 %}
|
|
|
|
This appears if variable somevar is greater than 1 or equal to 1.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``in`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
Contained within. This operator is supported by many Python containers to test
|
|
|
|
whether the given value is in the container. The following are some examples of
|
|
|
|
how ``x in y`` will be interpreted::
|
|
|
|
|
|
|
|
{% if "bc" in "abcdef" %}
|
|
|
|
This appears since "bc" is a substring of "abcdef"
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if "hello" in greetings %}
|
|
|
|
If greetings is a list or set, one element of which is the string
|
|
|
|
"hello", this will appear.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if user in users %}
|
|
|
|
If users is a QuerySet, this will appear if user is an
|
|
|
|
instance that belongs to the QuerySet.
|
|
|
|
{% endif %}
|
|
|
|
|
2010-03-09 07:25:37 +08:00
|
|
|
``not in`` operator
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Not contained within. This is the negation of the ``in`` operator.
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
|
|
|
|
The comparison operators cannot be 'chained' like in Python or in mathematical
|
|
|
|
notation. For example, instead of using::
|
|
|
|
|
|
|
|
{% if a > b > c %} (WRONG)
|
|
|
|
|
|
|
|
you should use::
|
|
|
|
|
|
|
|
{% if a > b and b > c %}
|
|
|
|
|
|
|
|
|
|
|
|
Filters
|
|
|
|
^^^^^^^
|
|
|
|
|
|
|
|
You can also use filters in the ``if`` expression. For example::
|
|
|
|
|
|
|
|
{% if messages|length >= 100 %}
|
|
|
|
You have lots of messages today!
|
2005-11-07 07:30:06 +08:00
|
|
|
{% endif %}
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
Complex expressions
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
All of the above can be combined to form complex expressions. For such
|
|
|
|
expressions, it can be important to know how the operators are grouped when the
|
|
|
|
expression is evaluated - that is, the precedence rules. The precedence of the
|
|
|
|
operators, from lowest to highest, is as follows:
|
|
|
|
|
|
|
|
* ``or``
|
|
|
|
* ``and``
|
|
|
|
* ``not``
|
|
|
|
* ``in``
|
|
|
|
* ``==``, ``!=``, ``<``, ``>``,``<=``, ``>=``
|
|
|
|
|
|
|
|
(This follows Python exactly). So, for example, the following complex if tag:
|
|
|
|
|
|
|
|
{% if a == b or c == d and e %}
|
|
|
|
|
|
|
|
...will be interpreted as:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
(a == b) or ((c == d) and e)
|
|
|
|
|
|
|
|
If you need different precedence, you will need to use nested if tags. Sometimes
|
|
|
|
that is better for clarity anyway, for the sake of those who do not know the
|
|
|
|
precedence rules.
|
2006-07-29 08:31:33 +08:00
|
|
|
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: ifchanged
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
ifchanged
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Check if a value has changed from the last iteration of a loop.
|
|
|
|
|
2006-11-07 13:36:51 +08:00
|
|
|
The 'ifchanged' block tag is used within a loop. It has two possible uses.
|
|
|
|
|
|
|
|
1. Checks its own rendered contents against its previous state and only
|
|
|
|
displays the content if it has changed. For example, this displays a list of
|
|
|
|
days, only displaying the month if it changes::
|
|
|
|
|
|
|
|
<h1>Archive for {{ year }}</h1>
|
|
|
|
|
|
|
|
{% for date in days %}
|
|
|
|
{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
|
|
|
|
<a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
|
|
|
|
{% endfor %}
|
2006-11-08 07:42:27 +08:00
|
|
|
|
2007-03-24 04:45:30 +08:00
|
|
|
2. If given a variable, check whether that variable has changed. For
|
|
|
|
example, the following shows the date every time it changes, but
|
2008-08-24 06:25:40 +08:00
|
|
|
only shows the hour if both the hour and the date has changed::
|
2006-11-08 07:42:27 +08:00
|
|
|
|
2006-11-07 13:36:51 +08:00
|
|
|
{% for date in days %}
|
2006-11-08 03:07:27 +08:00
|
|
|
{% ifchanged date.date %} {{ date.date }} {% endifchanged %}
|
2006-11-07 13:36:51 +08:00
|
|
|
{% ifchanged date.hour date.date %}
|
2006-11-08 03:07:27 +08:00
|
|
|
{{ date.hour }}
|
2006-11-07 13:36:51 +08:00
|
|
|
{% endifchanged %}
|
|
|
|
{% endfor %}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2009-04-16 20:46:15 +08:00
|
|
|
The ``ifchanged`` tag can also take an optional ``{% else %}`` clause that
|
|
|
|
will be displayed if the value has not changed::
|
|
|
|
|
|
|
|
{% for match in matches %}
|
|
|
|
<div style="background-color:
|
|
|
|
{% ifchanged match.ballot_id %}
|
2009-05-14 09:53:57 +08:00
|
|
|
{% cycle "red" "blue" %}
|
2009-04-16 20:46:15 +08:00
|
|
|
{% else %}
|
|
|
|
grey
|
|
|
|
{% endifchanged %}
|
|
|
|
">{{ match }}</div>
|
|
|
|
{% endfor %}
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: ifequal
|
2008-07-27 06:09:43 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
ifequal
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Output the contents of the block if the two arguments equal each other.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
{% ifequal user.id comment.user_id %}
|
|
|
|
...
|
|
|
|
{% endifequal %}
|
|
|
|
|
|
|
|
As in the ``{% if %}`` tag, an ``{% else %}`` clause is optional.
|
|
|
|
|
|
|
|
The arguments can be hard-coded strings, so the following is valid::
|
|
|
|
|
|
|
|
{% ifequal user.username "adrian" %}
|
|
|
|
...
|
|
|
|
{% endifequal %}
|
|
|
|
|
2006-09-28 19:20:48 +08:00
|
|
|
It is only possible to compare an argument to template variables or strings.
|
|
|
|
You cannot check for equality with Python objects such as ``True`` or
|
|
|
|
``False``. If you need to test if something is true or false, use the ``if``
|
2006-11-10 11:30:00 +08:00
|
|
|
tag instead.
|
2006-09-28 19:20:48 +08:00
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
.. versionadded:: 1.2
|
|
|
|
An alternative to the ``ifequal`` tag is to use the :ttag:`if` tag and the ``==`` operator.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: ifnotequal
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
ifnotequal
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Just like ``ifequal``, except it tests that the two arguments are not equal.
|
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
.. versionadded:: 1.2
|
|
|
|
An alternative to the ``ifnotequal`` tag is to use the :ttag:`if` tag and the ``!=`` operator.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: include
|
|
|
|
|
2005-11-22 13:44:04 +08:00
|
|
|
include
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Loads a template and renders it with the current context. This is a way of
|
|
|
|
"including" other templates within a template.
|
|
|
|
|
|
|
|
The template name can either be a variable or a hard-coded (quoted) string,
|
|
|
|
in either single or double quotes.
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
This example includes the contents of the template ``"foo/bar.html"``::
|
2005-11-22 13:44:04 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
{% include "foo/bar.html" %}
|
2005-11-22 13:44:04 +08:00
|
|
|
|
|
|
|
This example includes the contents of the template whose name is contained in
|
|
|
|
the variable ``template_name``::
|
|
|
|
|
|
|
|
{% include template_name %}
|
|
|
|
|
|
|
|
An included template is rendered with the context of the template that's
|
|
|
|
including it. This example produces the output ``"Hello, John"``:
|
|
|
|
|
|
|
|
* Context: variable ``person`` is set to ``"john"``.
|
|
|
|
* Template::
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
{% include "name_snippet.html" %}
|
2005-11-22 13:44:04 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
* The ``name_snippet.html`` template::
|
2005-11-22 13:44:04 +08:00
|
|
|
|
|
|
|
Hello, {{ person }}
|
|
|
|
|
|
|
|
See also: ``{% ssi %}``.
|
|
|
|
|
2010-05-01 22:27:10 +08:00
|
|
|
.. note::
|
|
|
|
The :ttag:`include` tag should be considered as an implementation of
|
|
|
|
"render this subtemplate and include the HTML", not as "parse this
|
|
|
|
subtemplate and include its contents as if it were part of the parent".
|
|
|
|
This means that there is no shared state between included templates --
|
|
|
|
each include is a completely independent rendering process.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: load
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
load
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
Load a custom template tag set.
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
See :doc:`Custom tag and filter libraries </howto/custom-template-tags>` for more information.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. templatetag:: now
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
now
|
|
|
|
~~~
|
|
|
|
|
|
|
|
Display the date, formatted according to the given string.
|
|
|
|
|
|
|
|
Uses the same format as PHP's ``date()`` function (http://php.net/date)
|
|
|
|
with some custom extensions.
|
|
|
|
|
|
|
|
Available format strings:
|
|
|
|
|
2005-11-07 07:36:28 +08:00
|
|
|
================ ======================================== =====================
|
|
|
|
Format character Description Example output
|
|
|
|
================ ======================================== =====================
|
|
|
|
a ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
|
2005-11-07 07:37:20 +08:00
|
|
|
this is slightly different than PHP's
|
|
|
|
output, because this includes periods
|
|
|
|
to match Associated Press style.)
|
2005-11-07 07:36:28 +08:00
|
|
|
A ``'AM'`` or ``'PM'``. ``'AM'``
|
2007-03-01 00:18:55 +08:00
|
|
|
b Month, textual, 3 letters, lowercase. ``'jan'``
|
2005-11-07 07:36:28 +08:00
|
|
|
B Not implemented.
|
2010-05-16 23:54:10 +08:00
|
|
|
c ISO 8601 Format. ``2008-01-02T10:30:00.000123``
|
2005-11-07 07:36:28 +08:00
|
|
|
d Day of the month, 2 digits with ``'01'`` to ``'31'``
|
2005-11-07 07:37:20 +08:00
|
|
|
leading zeros.
|
2005-11-07 07:36:28 +08:00
|
|
|
D Day of the week, textual, 3 letters. ``'Fri'``
|
|
|
|
f Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
|
2005-11-07 07:37:20 +08:00
|
|
|
with minutes left off if they're zero.
|
|
|
|
Proprietary extension.
|
2005-11-07 07:36:28 +08:00
|
|
|
F Month, textual, long. ``'January'``
|
|
|
|
g Hour, 12-hour format without leading ``'1'`` to ``'12'``
|
2005-11-07 07:37:20 +08:00
|
|
|
zeros.
|
2005-11-07 07:36:28 +08:00
|
|
|
G Hour, 24-hour format without leading ``'0'`` to ``'23'``
|
2005-11-07 07:37:20 +08:00
|
|
|
zeros.
|
2005-11-07 07:36:28 +08:00
|
|
|
h Hour, 12-hour format. ``'01'`` to ``'12'``
|
|
|
|
H Hour, 24-hour format. ``'00'`` to ``'23'``
|
|
|
|
i Minutes. ``'00'`` to ``'59'``
|
|
|
|
I Not implemented.
|
|
|
|
j Day of the month without leading ``'1'`` to ``'31'``
|
2005-11-07 07:37:20 +08:00
|
|
|
zeros.
|
2005-11-07 07:36:28 +08:00
|
|
|
l Day of the week, textual, long. ``'Friday'``
|
|
|
|
L Boolean for whether it's a leap year. ``True`` or ``False``
|
|
|
|
m Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
|
|
|
|
M Month, textual, 3 letters. ``'Jan'``
|
|
|
|
n Month without leading zeros. ``'1'`` to ``'12'``
|
|
|
|
N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
|
2005-11-07 07:37:20 +08:00
|
|
|
style. Proprietary extension.
|
2005-11-07 07:36:28 +08:00
|
|
|
O Difference to Greenwich time in hours. ``'+0200'``
|
|
|
|
P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
|
2005-11-07 07:37:20 +08:00
|
|
|
'a.m.'/'p.m.', with minutes left off
|
|
|
|
if they're zero and the special-case
|
|
|
|
strings 'midnight' and 'noon' if
|
|
|
|
appropriate. Proprietary extension.
|
2007-11-04 09:14:58 +08:00
|
|
|
r RFC 2822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'``
|
2005-11-07 07:36:28 +08:00
|
|
|
s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
|
|
|
|
S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
|
2005-11-07 07:37:20 +08:00
|
|
|
month, 2 characters.
|
2005-11-07 07:53:13 +08:00
|
|
|
t Number of days in the given month. ``28`` to ``31``
|
2005-11-07 07:36:28 +08:00
|
|
|
T Time zone of this machine. ``'EST'``, ``'MDT'``
|
2010-01-02 05:39:26 +08:00
|
|
|
u Microseconds. ``0`` to ``999999``
|
2009-05-08 21:39:37 +08:00
|
|
|
U Seconds since the Unix Epoch
|
|
|
|
(January 1 1970 00:00:00 UTC).
|
2005-11-07 07:36:28 +08:00
|
|
|
w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
|
2005-11-07 07:37:20 +08:00
|
|
|
leading zeros.
|
2008-01-09 15:32:05 +08:00
|
|
|
W ISO-8601 week number of year, with ``1``, ``53``
|
2005-11-07 07:37:20 +08:00
|
|
|
weeks starting on Monday.
|
2005-11-07 07:36:28 +08:00
|
|
|
y Year, 2 digits. ``'99'``
|
|
|
|
Y Year, 4 digits. ``'1999'``
|
|
|
|
z Day of the year. ``0`` to ``365``
|
|
|
|
Z Time zone offset in seconds. The ``-43200`` to ``43200``
|
2005-11-07 07:37:20 +08:00
|
|
|
offset for timezones west of UTC is
|
|
|
|
always negative, and for those east of
|
|
|
|
UTC is always positive.
|
2005-11-07 07:36:28 +08:00
|
|
|
================ ======================================== =====================
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
It is {% now "jS F Y H:i" %}
|
|
|
|
|
|
|
|
Note that you can backslash-escape a format string if you want to use the
|
|
|
|
"raw" value. In this example, "f" is backslash-escaped, because otherwise
|
|
|
|
"f" is a format string that displays the time. The "o" doesn't need to be
|
2007-04-12 19:12:45 +08:00
|
|
|
escaped, because it's not a format character::
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2005-12-02 04:27:34 +08:00
|
|
|
It is the {% now "jS o\f F" %}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2007-04-12 19:12:45 +08:00
|
|
|
This would display as "It is the 4th of September".
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2010-05-17 04:48:19 +08:00
|
|
|
.. versionadded:: 1.2
|
|
|
|
|
|
|
|
The ``c`` and ``u`` format specification characters were added in Django 1.2.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: regroup
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
regroup
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Regroup a list of alike objects by a common attribute.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
This complex tag is best illustrated by use of an example: say that ``people``
|
2007-07-16 05:16:32 +08:00
|
|
|
is a list of people represented by dictionaries with ``first_name``,
|
2008-09-09 09:54:20 +08:00
|
|
|
``last_name``, and ``gender`` keys:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-07-16 05:16:32 +08:00
|
|
|
|
|
|
|
people = [
|
|
|
|
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
|
|
|
|
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
|
|
|
|
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
|
|
|
|
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
|
|
|
|
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
|
|
|
|
]
|
|
|
|
|
|
|
|
...and you'd like to display a hierarchical list that is ordered by gender,
|
|
|
|
like this:
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
* Male:
|
|
|
|
* George Bush
|
|
|
|
* Bill Clinton
|
|
|
|
* Female:
|
|
|
|
* Margaret Thatcher
|
|
|
|
* Condoleezza Rice
|
|
|
|
* Unknown:
|
|
|
|
* Pat Smith
|
|
|
|
|
2007-07-16 05:16:32 +08:00
|
|
|
You can use the ``{% regroup %}`` tag to group the list of people by gender.
|
|
|
|
The following snippet of template code would accomplish this::
|
|
|
|
|
|
|
|
{% regroup people by gender as gender_list %}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
<ul>
|
2007-07-16 05:16:32 +08:00
|
|
|
{% for gender in gender_list %}
|
|
|
|
<li>{{ gender.grouper }}
|
2005-07-15 08:42:28 +08:00
|
|
|
<ul>
|
2007-07-16 05:16:32 +08:00
|
|
|
{% for item in gender.list %}
|
|
|
|
<li>{{ item.first_name }} {{ item.last_name }}</li>
|
2005-11-07 07:30:06 +08:00
|
|
|
{% endfor %}
|
2005-07-15 08:42:28 +08:00
|
|
|
</ul>
|
2007-03-28 06:30:59 +08:00
|
|
|
</li>
|
2005-11-07 07:30:06 +08:00
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-07-16 05:16:32 +08:00
|
|
|
Let's walk through this example. ``{% regroup %}`` takes three arguments: the
|
|
|
|
list you want to regroup, the attribute to group by, and the name of the
|
|
|
|
resulting list. Here, we're regrouping the ``people`` list by the ``gender``
|
|
|
|
attribute and calling the result ``gender_list``.
|
|
|
|
|
|
|
|
``{% regroup %}`` produces a list (in this case, ``gender_list``) of
|
|
|
|
**group objects**. Each group object has two attributes:
|
|
|
|
|
|
|
|
* ``grouper`` -- the item that was grouped by (e.g., the string "Male" or
|
|
|
|
"Female").
|
|
|
|
* ``list`` -- a list of all items in this group (e.g., a list of all people
|
|
|
|
with gender='Male').
|
|
|
|
|
|
|
|
Note that ``{% regroup %}`` does not order its input! Our example relies on
|
|
|
|
the fact that the ``people`` list was ordered by ``gender`` in the first place.
|
|
|
|
If the ``people`` list did *not* order its members by ``gender``, the regrouping
|
|
|
|
would naively display more than one group for a single gender. For example,
|
|
|
|
say the ``people`` list was set to this (note that the males are not grouped
|
2008-09-09 09:54:20 +08:00
|
|
|
together):
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-07-16 05:16:32 +08:00
|
|
|
|
|
|
|
people = [
|
|
|
|
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'},
|
|
|
|
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'},
|
|
|
|
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'},
|
|
|
|
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'},
|
|
|
|
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'},
|
|
|
|
]
|
|
|
|
|
|
|
|
With this input for ``people``, the example ``{% regroup %}`` template code
|
|
|
|
above would result in the following output:
|
|
|
|
|
|
|
|
* Male:
|
|
|
|
* Bill Clinton
|
|
|
|
* Unknown:
|
|
|
|
* Pat Smith
|
|
|
|
* Female:
|
|
|
|
* Margaret Thatcher
|
|
|
|
* Male:
|
|
|
|
* George Bush
|
|
|
|
* Female:
|
|
|
|
* Condoleezza Rice
|
|
|
|
|
|
|
|
The easiest solution to this gotcha is to make sure in your view code that the
|
|
|
|
data is ordered according to how you want to display it.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-07-16 05:16:32 +08:00
|
|
|
Another solution is to sort the data in the template using the ``dictsort``
|
|
|
|
filter, if your data is in a list of dictionaries::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-07-16 05:16:32 +08:00
|
|
|
{% regroup people|dictsort:"gender" by gender as gender_list %}
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: spaceless
|
|
|
|
|
2006-01-15 09:51:30 +08:00
|
|
|
spaceless
|
|
|
|
~~~~~~~~~
|
|
|
|
|
2007-04-01 09:09:21 +08:00
|
|
|
Removes whitespace between HTML tags. This includes tab
|
2006-01-15 13:18:17 +08:00
|
|
|
characters and newlines.
|
2006-01-15 09:51:30 +08:00
|
|
|
|
|
|
|
Example usage::
|
|
|
|
|
|
|
|
{% spaceless %}
|
|
|
|
<p>
|
|
|
|
<a href="foo/">Foo</a>
|
|
|
|
</p>
|
2006-01-17 05:44:10 +08:00
|
|
|
{% endspaceless %}
|
2006-01-15 09:51:30 +08:00
|
|
|
|
|
|
|
This example would return this HTML::
|
|
|
|
|
2007-04-01 09:09:21 +08:00
|
|
|
<p><a href="foo/">Foo</a></p>
|
2006-01-15 09:51:30 +08:00
|
|
|
|
2007-04-01 09:09:21 +08:00
|
|
|
Only space between *tags* is removed -- not space between tags and text. In
|
2006-01-15 09:51:30 +08:00
|
|
|
this example, the space around ``Hello`` won't be stripped::
|
|
|
|
|
|
|
|
{% spaceless %}
|
|
|
|
<strong>
|
|
|
|
Hello
|
|
|
|
</strong>
|
2006-01-17 05:44:10 +08:00
|
|
|
{% endspaceless %}
|
2006-01-15 09:51:30 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: ssi
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
ssi
|
|
|
|
~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Output the contents of a given file into the page.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Like a simple "include" tag, ``{% ssi %}`` includes the contents of another
|
|
|
|
file -- which must be specified using an absolute path -- in the current
|
|
|
|
page::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
{% ssi /home/html/ljworld.com/includes/right_generic.html %}
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
If the optional "parsed" parameter is given, the contents of the included
|
|
|
|
file are evaluated as template code, within the current context::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
{% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Note that if you use ``{% ssi %}``, you'll need to define
|
2008-08-24 06:25:40 +08:00
|
|
|
:setting:`ALLOWED_INCLUDE_ROOTS` in your Django settings, as a security measure.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-22 13:44:04 +08:00
|
|
|
See also: ``{% include %}``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: templatetag
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
templatetag
|
|
|
|
~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Output one of the syntax characters used to compose template tags.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Since the template system has no concept of "escaping", to display one of the
|
|
|
|
bits used in template tags, you must use the ``{% templatetag %}`` tag.
|
2005-08-30 05:58:21 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
The argument tells which template bit to output:
|
2005-08-30 05:58:21 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
================== =======
|
|
|
|
Argument Outputs
|
|
|
|
================== =======
|
|
|
|
``openblock`` ``{%``
|
|
|
|
``closeblock`` ``%}``
|
|
|
|
``openvariable`` ``{{``
|
|
|
|
``closevariable`` ``}}``
|
2006-06-18 12:12:55 +08:00
|
|
|
``openbrace`` ``{``
|
|
|
|
``closebrace`` ``}``
|
2006-10-27 09:58:13 +08:00
|
|
|
``opencomment`` ``{#``
|
|
|
|
``closecomment`` ``#}``
|
2005-11-07 07:30:06 +08:00
|
|
|
================== =======
|
2005-08-30 05:58:21 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: url
|
|
|
|
|
2007-02-13 12:24:58 +08:00
|
|
|
url
|
|
|
|
~~~
|
|
|
|
|
2007-02-13 14:13:06 +08:00
|
|
|
Returns an absolute URL (i.e., a URL without the domain name) matching a given
|
|
|
|
view function and optional parameters. This is a way to output links without
|
|
|
|
violating the DRY principle by having to hard-code URLs in your templates::
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2010-03-30 20:44:30 +08:00
|
|
|
{% url path.to.some_view v1 v2 %}
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2007-02-13 14:13:06 +08:00
|
|
|
The first argument is a path to a view function in the format
|
|
|
|
``package.package.module.function``. Additional arguments are optional and
|
2010-05-06 09:19:21 +08:00
|
|
|
should be space-separated values that will be used as arguments in the URL.
|
2010-03-02 12:04:23 +08:00
|
|
|
The example above shows passing positional arguments. Alternatively you may
|
|
|
|
use keyword syntax::
|
|
|
|
|
2010-03-30 20:44:30 +08:00
|
|
|
{% url path.to.some_view arg1=v1 arg2=v2 %}
|
2010-03-02 12:04:23 +08:00
|
|
|
|
2010-03-15 15:22:20 +08:00
|
|
|
Do not mix both positional and keyword syntax in a single call. All arguments
|
|
|
|
required by the URLconf should be present.
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2007-05-26 17:43:32 +08:00
|
|
|
For example, suppose you have a view, ``app_views.client``, whose URLconf
|
|
|
|
takes a client ID (here, ``client()`` is a method inside the views file
|
2008-09-09 09:54:20 +08:00
|
|
|
``app_views.py``). The URLconf line might look like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
('^client/(\d+)/$', 'app_views.client')
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2007-02-13 14:13:06 +08:00
|
|
|
If this app's URLconf is included into the project's URLconf under a path
|
2008-09-09 09:54:20 +08:00
|
|
|
such as this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-02-13 12:24:58 +08:00
|
|
|
|
|
|
|
('^clients/', include('project_name.app_name.urls'))
|
|
|
|
|
2007-02-13 14:13:06 +08:00
|
|
|
...then, in a template, you can create a link to this view like this::
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
{% url app_views.client client.id %}
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2007-02-13 14:13:06 +08:00
|
|
|
The template tag will output the string ``/clients/client/123/``.
|
2007-02-13 12:24:58 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
|
|
|
If you're using :ref:`named URL patterns <naming-url-patterns>`, you can
|
|
|
|
refer to the name of the pattern in the ``url`` tag instead of using the
|
|
|
|
path to the view.
|
2007-08-19 18:14:58 +08:00
|
|
|
|
2008-08-30 03:28:03 +08:00
|
|
|
Note that if the URL you're reversing doesn't exist, you'll get an
|
|
|
|
:exc:`NoReverseMatch` exception raised, which will cause your site to display an
|
|
|
|
error page.
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
.. versionadded:: 1.0
|
|
|
|
|
|
|
|
If you'd like to retrieve a URL without displaying it, you can use a slightly
|
|
|
|
different call::
|
2008-08-30 03:28:03 +08:00
|
|
|
|
|
|
|
|
2010-03-30 20:44:30 +08:00
|
|
|
{% url path.to.view arg arg2 as the_url %}
|
2009-02-22 14:09:28 +08:00
|
|
|
|
2008-08-30 03:28:03 +08:00
|
|
|
<a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
|
2009-02-22 14:09:28 +08:00
|
|
|
|
2008-08-30 03:28:03 +08:00
|
|
|
This ``{% url ... as var %}`` syntax will *not* cause an error if the view is
|
2008-09-09 09:54:20 +08:00
|
|
|
missing. In practice you'll use this to link to views that are optional::
|
2008-08-30 03:28:03 +08:00
|
|
|
|
|
|
|
{% url path.to.view as the_url %}
|
|
|
|
{% if the_url %}
|
|
|
|
<a href="{{ the_url }}">Link to optional stuff</a>
|
|
|
|
{% endif %}
|
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
|
|
|
If you'd like to retrieve a namespaced URL, specify the fully qualified name::
|
|
|
|
|
|
|
|
{% url myapp:view-name %}
|
|
|
|
|
|
|
|
This will follow the normal :ref:`namespaced URL resolution strategy
|
|
|
|
<topics-http-reversing-url-namespaces>`, including using any hints provided
|
|
|
|
by the context as to the current application.
|
|
|
|
|
2010-03-30 20:44:30 +08:00
|
|
|
.. versionchanged:: 1.2
|
|
|
|
|
|
|
|
For backwards compatibility, the ``{% url %}`` tag also supports the
|
|
|
|
use of commas to separate arguments. You shouldn't use this in any new
|
|
|
|
projects, but for the sake of the people who are still using it,
|
|
|
|
here's what it looks like::
|
|
|
|
|
|
|
|
{% url path.to.view arg,arg2 %}
|
|
|
|
{% url path.to.view arg, arg2 %}
|
|
|
|
|
|
|
|
This syntax doesn't support the use of literal commas, or or equals
|
|
|
|
signs. Did we mention you shouldn't use this syntax in any new
|
|
|
|
projects?
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: widthratio
|
2007-08-19 18:14:58 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
widthratio
|
|
|
|
~~~~~~~~~~
|
2005-08-30 05:58:21 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
For creating bar charts and such, this tag calculates the ratio of a given value
|
|
|
|
to a maximum value, and then applies that ratio to a constant.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
For example::
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Above, if ``this_value`` is 175 and ``max_value`` is 200, the image in the
|
2005-11-07 07:30:06 +08:00
|
|
|
above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5
|
|
|
|
which is rounded up to 88).
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatetag:: with
|
|
|
|
|
2007-03-28 01:25:56 +08:00
|
|
|
with
|
|
|
|
~~~~
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2007-03-28 01:25:56 +08:00
|
|
|
|
2007-03-28 01:30:57 +08:00
|
|
|
Caches a complex variable under a simpler name. This is useful when accessing
|
2007-03-28 03:02:32 +08:00
|
|
|
an "expensive" method (e.g., one that hits the database) multiple times.
|
2007-03-28 01:25:56 +08:00
|
|
|
|
|
|
|
For example::
|
|
|
|
|
2007-03-28 01:30:57 +08:00
|
|
|
{% with business.employees.count as total %}
|
|
|
|
{{ total }} employee{{ total|pluralize }}
|
2007-03-28 01:25:56 +08:00
|
|
|
{% endwith %}
|
|
|
|
|
|
|
|
The populated variable (in the example above, ``total``) is only available
|
2007-03-28 03:02:32 +08:00
|
|
|
between the ``{% with %}`` and ``{% endwith %}`` tags.
|
2007-03-28 01:25:56 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. _ref-templates-builtins-filters:
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Built-in filter reference
|
|
|
|
-------------------------
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: add
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
add
|
|
|
|
~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Adds the argument to the value.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
{{ value|add:"2" }}
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``4``, then the output will be ``6``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2010-02-27 01:05:52 +08:00
|
|
|
.. versionchanged:: 1.2
|
2010-02-23 07:34:33 +08:00
|
|
|
The following behavior didn't exist in previous Django versions.
|
|
|
|
|
|
|
|
This filter will first try to coerce both values to integers. If this fails,
|
|
|
|
it'll attempt to add the values together anyway. This will work on some data
|
|
|
|
types (strings, list, etc.) and fail on others. If it fails, the result will
|
|
|
|
be an empty string.
|
|
|
|
|
|
|
|
For example, if we have::
|
|
|
|
|
|
|
|
{{ first|add:second }}
|
|
|
|
|
|
|
|
and ``first`` is ``[1, 2, 3]`` and ``second`` is ``[4, 5, 6]``, then the
|
|
|
|
output will be ``[1, 2, 3, 4, 5, 6]``.
|
|
|
|
|
|
|
|
.. warning::
|
2010-02-27 01:05:52 +08:00
|
|
|
|
2010-02-23 07:34:33 +08:00
|
|
|
Keep in mind that strings that can both be coerced to integers will be,
|
|
|
|
and thus will be will be *summed*, not concatenated, as in the first
|
|
|
|
example above.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: addslashes
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
addslashes
|
|
|
|
~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-12-05 05:08:29 +08:00
|
|
|
Adds slashes before quotes. Useful for escaping strings in CSV, for example.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|addslashes }}
|
|
|
|
|
|
|
|
If ``value`` is ``"I'm using Django"``, the output will be ``"I\'m using Django"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: capfirst
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
capfirst
|
|
|
|
~~~~~~~~
|
2005-10-21 05:58:01 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Capitalizes the first character of the value.
|
2005-10-21 05:58:01 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|capfirst }}
|
|
|
|
|
|
|
|
If ``value`` is ``"django"``, the output will be ``"Django"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: center
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
center
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Centers the value in a field of a given width.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
"{{ value|center:"15" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``"Django"``, the output will be ``" Django "``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: cut
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
cut
|
|
|
|
~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Removes all values of arg from the given string.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|cut:" "}}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"String with spaces"``, the output will be ``"Stringwithspaces"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: date
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
date
|
|
|
|
~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
Formats a date according to the given format.
|
|
|
|
|
|
|
|
Given format can be one of the predefined ones ``DATE_FORMAT``,
|
|
|
|
``DATETIME_FORMAT``, ``SHORT_DATE_FORMAT`` or ``SHORT_DATETIME_FORMAT``,
|
2010-05-17 04:48:19 +08:00
|
|
|
or a custom format, same as the :ttag:`now` tag. Note that predefined formats
|
|
|
|
may vary depending on the current locale.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|date:"D d M Y" }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is a ``datetime`` object (e.g., the result of
|
|
|
|
``datetime.datetime.now()``), the output will be the string
|
|
|
|
``'Wed 09 Jan 2008'``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2010-02-27 01:05:52 +08:00
|
|
|
Another example:
|
|
|
|
|
|
|
|
Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
|
|
|
|
for example, ``"es"``, then for::
|
|
|
|
|
|
|
|
{{ value|date:"SHORT_DATE_FORMAT" }}
|
|
|
|
|
|
|
|
the output will be the string ``"09/01/2008"`` (The ``"SHORT_DATE_FORMAT"``
|
|
|
|
format specifier for the ``es`` locale as shipped with Django is ``"d/m/Y"``).
|
|
|
|
|
2009-04-01 00:34:24 +08:00
|
|
|
When used without a format string::
|
|
|
|
|
|
|
|
{{ value|date }}
|
|
|
|
|
|
|
|
...the formatting string defined in the :setting:`DATE_FORMAT` setting will be
|
2009-12-23 01:58:49 +08:00
|
|
|
used, without applying any localization.
|
2009-04-01 00:34:24 +08:00
|
|
|
|
2010-02-27 01:05:52 +08:00
|
|
|
.. versionchanged:: 1.2
|
|
|
|
Predefined formats can now be influenced by the current locale.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: default
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
default
|
|
|
|
~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If value evaluates to ``False``, use given default. Otherwise, use the value.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|default:"nothing" }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``""`` (the empty string), the output will be ``nothing``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: default_if_none
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
default_if_none
|
|
|
|
~~~~~~~~~~~~~~~
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If (and only if) value is ``None``, use given default. Otherwise, use the
|
|
|
|
value.
|
|
|
|
|
|
|
|
Note that if an empty string is given, the default value will *not* be used.
|
|
|
|
Use the ``default`` filter if you want to fallback for empty strings.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|default_if_none:"nothing" }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``None``, the output will be the string ``"nothing"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: dictsort
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
dictsort
|
|
|
|
~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Takes a list of dictionaries and returns that list sorted by the key given in
|
2007-07-16 05:16:32 +08:00
|
|
|
the argument.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|dictsort:"name" }}
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
If ``value`` is:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2008-03-18 12:21:08 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
[
|
2008-03-18 12:21:08 +08:00
|
|
|
{'name': 'zed', 'age': 19},
|
|
|
|
{'name': 'amy', 'age': 22},
|
|
|
|
{'name': 'joe', 'age': 31},
|
2008-03-18 08:14:41 +08:00
|
|
|
]
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
then the output would be:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2008-03-18 08:14:41 +08:00
|
|
|
|
|
|
|
[
|
2008-03-18 12:21:08 +08:00
|
|
|
{'name': 'amy', 'age': 22},
|
|
|
|
{'name': 'joe', 'age': 31},
|
|
|
|
{'name': 'zed', 'age': 19},
|
2008-03-18 08:14:41 +08:00
|
|
|
]
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: dictsortreversed
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
dictsortreversed
|
|
|
|
~~~~~~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Takes a list of dictionaries and returns that list sorted in reverse order by
|
|
|
|
the key given in the argument. This works exactly the same as the above filter,
|
|
|
|
but the returned value will be in reverse order.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: divisibleby
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
divisibleby
|
|
|
|
~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Returns ``True`` if the value is divisible by the argument.
|
2005-10-14 11:48:27 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
{{ value|divisibleby:"3" }}
|
2008-03-18 08:14:41 +08:00
|
|
|
|
|
|
|
If ``value`` is ``21``, the output would be ``True``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: escape
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
escape
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Escapes a string's HTML. Specifically, it makes these replacements:
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-12-02 02:38:12 +08:00
|
|
|
* ``<`` is converted to ``<``
|
|
|
|
* ``>`` is converted to ``>``
|
|
|
|
* ``'`` (single quote) is converted to ``'``
|
|
|
|
* ``"`` (double quote) is converted to ``"``
|
|
|
|
* ``&`` is converted to ``&``
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
The escaping is only applied when the string is output, so it does not matter
|
|
|
|
where in a chained sequence of filters you put ``escape``: it will always be
|
|
|
|
applied as though it were the last filter. If you want escaping to be applied
|
|
|
|
immediately, use the ``force_escape`` filter.
|
|
|
|
|
|
|
|
Applying ``escape`` to a variable that would normally have auto-escaping
|
|
|
|
applied to the result will only result in one round of escaping being done. So
|
|
|
|
it is safe to use this function even in auto-escaping environments. If you want
|
|
|
|
multiple escaping passes to be applied, use the ``force_escape`` filter.
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionchanged:: 1.0
|
|
|
|
Due to auto-escaping, the behavior of this filter has changed slightly.
|
|
|
|
The replacements are only made once, after
|
|
|
|
all other filters are applied -- including filters before and after it.
|
2008-03-18 12:21:08 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: escapejs
|
|
|
|
|
2007-12-05 05:08:29 +08:00
|
|
|
escapejs
|
|
|
|
~~~~~~~~
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2007-12-05 05:08:29 +08:00
|
|
|
|
|
|
|
Escapes characters for use in JavaScript strings. This does *not* make the
|
|
|
|
string safe for use in HTML, but does protect you from syntax errors when using
|
|
|
|
templates to generate JavaScript/JSON.
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|escapejs }}
|
|
|
|
|
|
|
|
If ``value`` is ``"testing\r\njavascript \'string" <b>escaping</b>"``,
|
2010-03-15 15:22:20 +08:00
|
|
|
the output will be ``"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 \\u003Cb\\u003Eescaping\\u003C/b\\u003E"``.
|
2010-01-13 07:36:19 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: filesizeformat
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
filesizeformat
|
|
|
|
~~~~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
Format the value like a 'human-readable' file size (i.e. ``'13 KB'``,
|
|
|
|
``'4.1 MB'``, ``'102 bytes'``, etc).
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|filesizeformat }}
|
|
|
|
|
|
|
|
If ``value`` is 123456789, the output would be ``117.7 MB``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: first
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
first
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Returns the first item in a list.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|first }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c']``, the output will be ``'a'``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: fix_ampersands
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
fix_ampersands
|
|
|
|
~~~~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionchanged:: 1.0
|
|
|
|
This is rarely useful as ampersands are now automatically escaped. See escape_ for more information.
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Replaces ampersands with ``&`` entities.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|fix_ampersands }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``Tom & Jerry``, the output will be ``Tom & Jerry``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: floatformat
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
floatformat
|
2005-11-22 12:42:26 +08:00
|
|
|
~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-01-03 13:29:34 +08:00
|
|
|
When used without an argument, rounds a floating-point number to one decimal
|
|
|
|
place -- but only if there's a decimal part to be displayed. For example:
|
2005-11-22 12:44:21 +08:00
|
|
|
|
2007-12-11 14:55:25 +08:00
|
|
|
============ =========================== ========
|
|
|
|
``value`` Template Output
|
|
|
|
============ =========================== ========
|
|
|
|
``34.23234`` ``{{ value|floatformat }}`` ``34.2``
|
|
|
|
``34.00000`` ``{{ value|floatformat }}`` ``34``
|
|
|
|
``34.26000`` ``{{ value|floatformat }}`` ``34.3``
|
|
|
|
============ =========================== ========
|
2007-11-04 10:48:44 +08:00
|
|
|
|
|
|
|
If used with a numeric integer argument, ``floatformat`` rounds a number to
|
2007-12-02 01:29:45 +08:00
|
|
|
that many decimal places. For example:
|
2007-11-04 10:48:44 +08:00
|
|
|
|
2007-12-11 14:55:25 +08:00
|
|
|
============ ============================= ==========
|
|
|
|
``value`` Template Output
|
|
|
|
============ ============================= ==========
|
|
|
|
``34.23234`` ``{{ value|floatformat:3 }}`` ``34.232``
|
|
|
|
``34.00000`` ``{{ value|floatformat:3 }}`` ``34.000``
|
|
|
|
``34.26000`` ``{{ value|floatformat:3 }}`` ``34.260``
|
|
|
|
============ ============================= ==========
|
2007-11-04 10:48:44 +08:00
|
|
|
|
|
|
|
If the argument passed to ``floatformat`` is negative, it will round a number
|
|
|
|
to that many decimal places -- but only if there's a decimal part to be
|
|
|
|
displayed. For example:
|
|
|
|
|
2007-12-11 14:55:25 +08:00
|
|
|
============ ================================ ==========
|
|
|
|
``value`` Template Output
|
|
|
|
============ ================================ ==========
|
|
|
|
``34.23234`` ``{{ value|floatformat:"-3" }}`` ``34.232``
|
|
|
|
``34.00000`` ``{{ value|floatformat:"-3" }}`` ``34``
|
|
|
|
``34.26000`` ``{{ value|floatformat:"-3" }}`` ``34.260``
|
|
|
|
============ ================================ ==========
|
2007-11-04 10:48:44 +08:00
|
|
|
|
|
|
|
Using ``floatformat`` with no argument is equivalent to using ``floatformat``
|
|
|
|
with an argument of ``-1``.
|
2007-01-03 13:29:34 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: force_escape
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
force_escape
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
Applies HTML escaping to a string (see the ``escape`` filter for details).
|
|
|
|
This filter is applied *immediately* and returns a new, escaped string. This
|
|
|
|
is useful in the rare cases where you need multiple escaping or want to apply
|
|
|
|
other filters to the escaped results. Normally, you want to use the ``escape``
|
|
|
|
filter.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: get_digit
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
get_digit
|
|
|
|
~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Given a whole number, returns the requested digit, where 1 is the right-most
|
|
|
|
digit, 2 is the second-right-most digit, etc. Returns the original value for
|
|
|
|
invalid input (if input or argument is not an integer, or if argument is less
|
|
|
|
than 1). Otherwise, output is always an integer.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
{{ value|get_digit:"2" }}
|
|
|
|
|
|
|
|
If ``value`` is ``123456789``, the output will be ``8``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2010-05-07 08:57:17 +08:00
|
|
|
.. templatefilter:: iriencode
|
2008-08-24 06:25:40 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
iriencode
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Converts an IRI (Internationalized Resource Identifier) to a string that is
|
|
|
|
suitable for including in a URL. This is necessary if you're trying to use
|
|
|
|
strings containing non-ASCII characters in a URL.
|
|
|
|
|
|
|
|
It's safe to use this filter on a string that has already gone through the
|
|
|
|
``urlencode`` filter.
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|iriencode }}
|
|
|
|
|
|
|
|
If ``value`` is ``"?test=1&me=2"``, the output will be ``"?test=1&me=2"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: join
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
join
|
|
|
|
~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
Joins a list with a string, like Python's ``str.join(list)``
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|join:" // " }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c']``, the output will be the string
|
|
|
|
``"a // b // c"``.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: last
|
|
|
|
|
2008-01-06 11:53:33 +08:00
|
|
|
last
|
|
|
|
~~~~
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionadded:: 1.0
|
2008-02-16 13:15:09 +08:00
|
|
|
|
2008-01-06 11:53:33 +08:00
|
|
|
Returns the last item in a list.
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|last }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output will be the string
|
|
|
|
``"d"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: length
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
length
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Returns the length of the value. This works for both strings and lists.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|length }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``4``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: length_is
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
length_is
|
|
|
|
~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Returns ``True`` if the value's length is the argument, or ``False`` otherwise.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
{{ value|length_is:"4" }}
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``['a', 'b', 'c', 'd']``, the output will be ``True``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: linebreaks
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
linebreaks
|
|
|
|
~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-09-15 05:46:38 +08:00
|
|
|
Replaces line breaks in plain text with appropriate HTML; a single
|
|
|
|
newline becomes an HTML line break (``<br />``) and a new line
|
|
|
|
followed by a blank line becomes a paragraph break (``</p>``).
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|linebreaks }}
|
|
|
|
|
2009-02-21 16:43:35 +08:00
|
|
|
If ``value`` is ``Joel\nis a slug``, the output will be ``<p>Joel<br />is a
|
2008-03-18 08:14:41 +08:00
|
|
|
slug</p>``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: linebreaksbr
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
linebreaksbr
|
|
|
|
~~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-09-15 05:46:38 +08:00
|
|
|
Converts all newlines in a piece of plain text to HTML line breaks
|
|
|
|
(``<br />``).
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|linebreaksbr }}
|
|
|
|
|
|
|
|
If ``value`` is ``Joel\nis a slug``, the output will be ``Joel<br />is a
|
|
|
|
slug``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: linenumbers
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
linenumbers
|
|
|
|
~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Displays text with line numbers.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|linenumbers }}
|
|
|
|
|
|
|
|
If ``value`` is::
|
|
|
|
|
|
|
|
one
|
|
|
|
two
|
|
|
|
three
|
|
|
|
|
|
|
|
the output will be::
|
|
|
|
|
|
|
|
1. one
|
|
|
|
2. two
|
|
|
|
3. three
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: ljust
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
ljust
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Left-aligns the value in a field of a given width.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
**Argument:** field size
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
"{{ value|ljust:"10" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``Django``, the output will be ``"Django "``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: lower
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
lower
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Converts a string into all lowercase.
|
2005-09-25 01:39:20 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|lower }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``Still MAD At Yoko``, the output will be ``still mad at yoko``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: make_list
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
make_list
|
|
|
|
~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Returns the value turned into a list. For an integer, it's a list of
|
|
|
|
digits. For a string, it's a list of characters.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|make_list }}
|
|
|
|
|
2008-07-26 11:08:53 +08:00
|
|
|
If ``value`` is the string ``"Joel"``, the output would be the list
|
|
|
|
``[u'J', u'o', u'e', u'l']``. If ``value`` is ``123``, the output will be the
|
|
|
|
list ``[1, 2, 3]``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: phone2numeric
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
phone2numeric
|
|
|
|
~~~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-02-13 23:33:25 +08:00
|
|
|
Converts a phone number (possibly containing letters) to its numerical
|
2010-01-13 07:36:19 +08:00
|
|
|
equivalent.
|
2006-02-13 23:33:25 +08:00
|
|
|
|
|
|
|
The input doesn't have to be a valid phone number. This will happily convert
|
|
|
|
any string.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|phone2numeric }}
|
|
|
|
|
|
|
|
If ``value`` is ``800-COLLECT``, the output will be ``800-2655328``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: pluralize
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
pluralize
|
|
|
|
~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-08-22 02:27:33 +08:00
|
|
|
Returns a plural suffix if the value is not 1. By default, this suffix is ``'s'``.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
You have {{ num_messages }} message{{ num_messages|pluralize }}.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
If ``num_messages`` is ``1``, the output will be ``You have 1 message.``
|
|
|
|
If ``num_messages`` is ``2`` the output will be ``You have 2 messages.``
|
|
|
|
|
2006-08-22 02:27:33 +08:00
|
|
|
For words that require a suffix other than ``'s'``, you can provide an alternate
|
2006-07-04 14:18:39 +08:00
|
|
|
suffix as a parameter to the filter.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
2009-05-28 00:26:56 +08:00
|
|
|
You have {{ num_walruses }} walrus{{ num_walruses|pluralize:"es" }}.
|
2006-07-04 14:18:39 +08:00
|
|
|
|
|
|
|
For words that don't pluralize by simple suffix, you can specify both a
|
|
|
|
singular and plural suffix, separated by a comma.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
|
2009-02-22 14:09:28 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: pprint
|
2006-07-04 14:18:39 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
pprint
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
A wrapper around `pprint.pprint`__ -- for debugging, really.
|
|
|
|
|
2008-11-02 03:02:09 +08:00
|
|
|
__ http://docs.python.org/library/pprint.html
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: random
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
random
|
|
|
|
~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Returns a random item from the given list.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|random }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output could be ``"b"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: removetags
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
removetags
|
|
|
|
~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Removes a space-separated list of [X]HTML tags from the output.
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|removetags:"b span"|safe }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"`` the
|
|
|
|
output will be ``"Joel <button>is</button> a slug"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: rjust
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
rjust
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
Right-aligns the value in a field of a given width.
|
|
|
|
|
|
|
|
**Argument:** field size
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
"{{ value|rjust:"10" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``Django``, the output will be ``" Django"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: safe
|
|
|
|
|
2007-11-14 20:58:53 +08:00
|
|
|
safe
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
Marks a string as not requiring further HTML escaping prior to output. When
|
|
|
|
autoescaping is off, this filter has no effect.
|
|
|
|
|
2010-05-09 14:45:21 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you are chaining filters, a filter applied after ``safe`` can
|
|
|
|
make the contents unsafe again. For example, the following code
|
|
|
|
prints the variable as is, unescaped:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{{ var|safe|escape }}
|
|
|
|
|
2009-03-02 16:16:33 +08:00
|
|
|
.. templatefilter:: safeseq
|
|
|
|
|
|
|
|
safeseq
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Applies the :tfilter:`safe` filter to each element of a sequence. Useful in
|
|
|
|
conjunction with other filters that operate on sequences, such as
|
|
|
|
:tfilter:`join`. For example::
|
|
|
|
|
|
|
|
{{ some_list|safeseq|join:", " }}
|
|
|
|
|
|
|
|
You couldn't use the :tfilter:`safe` filter directly in this case, as it would
|
|
|
|
first convert the variable into a string, rather than working with the
|
|
|
|
individual elements of the sequence.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: slice
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
slice
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
Returns a slice of the list.
|
|
|
|
|
|
|
|
Uses the same syntax as Python's list slicing. See
|
|
|
|
http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice
|
|
|
|
for an introduction.
|
|
|
|
|
2008-09-09 09:54:20 +08:00
|
|
|
Example::
|
|
|
|
|
|
|
|
{{ some_list|slice:":2" }}
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
If ``some_list`` is ``['a', 'b', 'c']``, the output will be ``['a', 'b']``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: slugify
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
slugify
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
Converts to lowercase, removes non-word characters (alphanumerics and
|
|
|
|
underscores) and converts spaces to hyphens. Also strips leading and trailing
|
|
|
|
whitespace.
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|slugify }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: stringformat
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
stringformat
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Formats the variable according to the argument, a string formatting specifier.
|
2008-08-24 06:25:40 +08:00
|
|
|
This specifier uses Python string formatting syntax, with the exception that
|
2005-11-07 07:30:06 +08:00
|
|
|
the leading "%" is dropped.
|
|
|
|
|
2009-02-22 14:09:28 +08:00
|
|
|
See http://docs.python.org/library/stdtypes.html#string-formatting-operations
|
|
|
|
for documentation of Python string formatting
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|stringformat:"s" }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is a slug"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: striptags
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
striptags
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Strips all [X]HTML tags.
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|striptags }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"<b>Joel</b> <button>is</button> a <span>slug</span>"``, the
|
|
|
|
output will be ``"Joel is a slug"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: time
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
time
|
|
|
|
~~~~
|
|
|
|
|
2009-12-23 01:58:49 +08:00
|
|
|
Formats a time according to the given format.
|
|
|
|
|
|
|
|
Given format can be the predefined one ``TIME_FORMAT``, or a custom format,
|
2010-05-17 04:48:19 +08:00
|
|
|
same as the :ttag:`now` tag. Note that the predefined format is locale-
|
|
|
|
dependant.
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2007-07-12 22:11:41 +08:00
|
|
|
The time filter will only accept parameters in the format string that relate
|
|
|
|
to the time of day, not the date (for obvious reasons). If you need to
|
2010-05-17 04:48:19 +08:00
|
|
|
format a date, use the :tfilter:`date` filter.
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|time:"H:i" }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is equivalent to ``datetime.datetime.now()``, the output will be
|
|
|
|
the string ``"01:23"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2010-02-27 01:05:52 +08:00
|
|
|
Another example:
|
|
|
|
|
|
|
|
Assuming that :setting:`USE_L10N` is ``True`` and :setting:`LANGUAGE_CODE` is,
|
|
|
|
for example, ``"de"``, then for::
|
|
|
|
|
|
|
|
{{ value|time:"TIME_FORMAT" }}
|
|
|
|
|
|
|
|
the output will be the string ``"01:23:00"`` (The ``"TIME_FORMAT"`` format
|
|
|
|
specifier for the ``de`` locale as shipped with Django is ``"H:i:s"``).
|
|
|
|
|
2009-04-01 00:34:24 +08:00
|
|
|
When used without a format string::
|
|
|
|
|
|
|
|
{{ value|time }}
|
|
|
|
|
|
|
|
...the formatting string defined in the :setting:`TIME_FORMAT` setting will be
|
2010-02-27 01:05:52 +08:00
|
|
|
used, without applying any localization.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.2
|
|
|
|
Predefined formats can now be influenced by the current locale.
|
2009-04-01 00:34:24 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: timesince
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
timesince
|
|
|
|
~~~~~~~~~
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
Formats a date as the time since that date (e.g., "4 days, 6 hours").
|
2005-11-07 07:30:06 +08:00
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
Takes an optional argument that is a variable containing the date to use as
|
|
|
|
the comparison point (without the argument, the comparison point is *now*).
|
|
|
|
For example, if ``blog_date`` is a date instance representing midnight on 1
|
2006-09-29 10:30:42 +08:00
|
|
|
June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
|
2008-08-25 20:31:10 +08:00
|
|
|
then ``{{ blog_date|timesince:comment_date }}`` would return "8 hours".
|
2006-06-21 14:56:08 +08:00
|
|
|
|
2008-08-26 16:08:55 +08:00
|
|
|
Comparing offset-naive and offset-aware datetimes will return an empty string.
|
|
|
|
|
2007-09-17 12:50:12 +08:00
|
|
|
Minutes is the smallest unit used, and "0 minutes" will be returned for any
|
|
|
|
date that is in the future relative to the comparison point.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: timeuntil
|
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
timeuntil
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Similar to ``timesince``, except that it measures the time from now until the
|
|
|
|
given date or datetime. For example, if today is 1 June 2006 and
|
|
|
|
``conference_date`` is a date instance holding 29 June 2006, then
|
2007-09-17 12:50:12 +08:00
|
|
|
``{{ conference_date|timeuntil }}`` will return "4 weeks".
|
2006-06-21 14:56:08 +08:00
|
|
|
|
|
|
|
Takes an optional argument that is a variable containing the date to use as
|
|
|
|
the comparison point (instead of *now*). If ``from_date`` contains 22 June
|
2007-09-17 12:50:12 +08:00
|
|
|
2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week".
|
|
|
|
|
2008-08-26 16:08:55 +08:00
|
|
|
Comparing offset-naive and offset-aware datetimes will return an empty string.
|
|
|
|
|
2007-09-17 12:50:12 +08:00
|
|
|
Minutes is the smallest unit used, and "0 minutes" will be returned for any
|
|
|
|
date that is in the past relative to the comparison point.
|
2006-06-21 14:56:08 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: title
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
title
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
Converts a string into titlecase.
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|title }}
|
|
|
|
|
|
|
|
If ``value`` is ``"my first post"``, the output will be ``"My First Post"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: truncatewords
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
truncatewords
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Truncates a string after a certain number of words.
|
|
|
|
|
|
|
|
**Argument:** Number of words to truncate after
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|truncatewords:2 }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is ..."``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2010-08-07 22:57:24 +08:00
|
|
|
Newlines within the string will be removed.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: truncatewords_html
|
|
|
|
|
2007-02-10 13:22:12 +08:00
|
|
|
truncatewords_html
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Similar to ``truncatewords``, except that it is aware of HTML tags. Any tags
|
|
|
|
that are opened in the string and not closed before the truncation point, are
|
|
|
|
closed immediately after the truncation.
|
|
|
|
|
|
|
|
This is less efficient than ``truncatewords``, so should only be used when it
|
|
|
|
is being passed HTML text.
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|truncatewords_html:2 }}
|
|
|
|
|
|
|
|
If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
|
|
|
|
``"<p>Joel is ...</p>"``.
|
|
|
|
|
2010-08-07 22:57:24 +08:00
|
|
|
Newlines in the HTML content will be preserved.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: unordered_list
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
unordered_list
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
|
|
|
WITHOUT opening and closing <ul> tags.
|
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. versionchanged:: 1.0
|
|
|
|
The format accepted by ``unordered_list`` has changed to be easier to understand.
|
2007-08-26 09:11:20 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
The list is assumed to be in the proper format. For example, if ``var`` contains
|
2007-08-26 09:11:20 +08:00
|
|
|
``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then
|
|
|
|
``{{ var|unordered_list }}`` would return::
|
2005-11-07 07:30:06 +08:00
|
|
|
|
|
|
|
<li>States
|
|
|
|
<ul>
|
|
|
|
<li>Kansas
|
|
|
|
<ul>
|
|
|
|
<li>Lawrence</li>
|
|
|
|
<li>Topeka</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li>Illinois</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
2007-08-26 09:11:20 +08:00
|
|
|
Note: the previous more restrictive and verbose format is still supported:
|
|
|
|
``['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]]``,
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: upper
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
upper
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
Converts a string into all uppercase.
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|upper }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"JOEL IS A SLUG"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: urlencode
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
urlencode
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Escapes a value for use in a URL.
|
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|urlencode }}
|
|
|
|
|
|
|
|
If ``value`` is ``"http://www.example.org/foo?a=b&c=d"``, the output will be
|
|
|
|
``"http%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
|
|
|
|
|
2010-09-14 07:01:34 +08:00
|
|
|
.. versionadded:: 1.1
|
|
|
|
|
|
|
|
An optional argument containing the characters which should not be escaped can
|
|
|
|
be provided.
|
|
|
|
|
|
|
|
If not provided, the '/' character is assumed safe. An empty string can be
|
|
|
|
provided when *all* characters should be escaped. For example::
|
|
|
|
|
|
|
|
{{ value|urlencode:"" }}
|
|
|
|
|
|
|
|
If ``value`` is ``"http://www.example.org/"``, the output will be
|
|
|
|
``"http%3A%2F%2Fwww.example.org%2F"``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: urlize
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
urlize
|
|
|
|
~~~~~~
|
|
|
|
|
|
|
|
Converts URLs in plain text into clickable links.
|
|
|
|
|
2007-08-19 16:50:51 +08:00
|
|
|
Note that if ``urlize`` is applied to text that already contains HTML markup,
|
|
|
|
things won't work as expected. Apply this filter only to *plain* text.
|
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|urlize }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Check out www.djangoproject.com"``, the output will be
|
|
|
|
``"Check out <a
|
|
|
|
href="http://www.djangoproject.com">www.djangoproject.com</a>"``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: urlizetrunc
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
urlizetrunc
|
|
|
|
~~~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-06-23 11:10:32 +08:00
|
|
|
Converts URLs into clickable links, truncating URLs longer than the given
|
|
|
|
character limit.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2007-08-19 16:50:51 +08:00
|
|
|
As with urlize_, this filter should only be applied to *plain* text.
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
**Argument:** Length to truncate URLs to
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|urlizetrunc:15 }}
|
|
|
|
|
2008-03-18 12:21:08 +08:00
|
|
|
If ``value`` is ``"Check out www.djangoproject.com"``, the output would be
|
|
|
|
``'Check out <a
|
|
|
|
href="http://www.djangoproject.com">www.djangopr...</a>'``.
|
2008-03-18 08:14:41 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: wordcount
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
wordcount
|
|
|
|
~~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Returns the number of words.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2010-01-13 07:36:19 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|wordcount }}
|
|
|
|
|
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``4``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: wordwrap
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
wordwrap
|
|
|
|
~~~~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Wraps words at specified line length.
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2006-03-02 13:53:58 +08:00
|
|
|
**Argument:** number of characters at which to wrap the text
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2008-03-18 08:14:41 +08:00
|
|
|
For example::
|
|
|
|
|
|
|
|
{{ value|wordwrap:5 }}
|
|
|
|
|
|
|
|
If ``value`` is ``Joel is a slug``, the output would be::
|
|
|
|
|
|
|
|
Joel
|
|
|
|
is a
|
|
|
|
slug
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. templatefilter:: yesno
|
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
yesno
|
|
|
|
~~~~~
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
Given a string mapping values for true, false and (optionally) None,
|
|
|
|
returns one of those strings according to the value:
|
2005-07-17 23:15:26 +08:00
|
|
|
|
2005-11-07 07:30:06 +08:00
|
|
|
========== ====================== ==================================
|
|
|
|
Value Argument Outputs
|
|
|
|
========== ====================== ==================================
|
|
|
|
``True`` ``"yeah,no,maybe"`` ``yeah``
|
|
|
|
``False`` ``"yeah,no,maybe"`` ``no``
|
|
|
|
``None`` ``"yeah,no,maybe"`` ``maybe``
|
|
|
|
``None`` ``"yeah,no"`` ``"no"`` (converts None to False
|
|
|
|
if no mapping for None is given)
|
|
|
|
========== ====================== ==================================
|
2006-06-04 08:58:39 +08:00
|
|
|
|
|
|
|
Other tags and filter libraries
|
2008-11-25 06:01:24 +08:00
|
|
|
-------------------------------
|
2006-06-04 08:58:39 +08:00
|
|
|
|
|
|
|
Django comes with a couple of other template-tag libraries that you have to
|
|
|
|
enable explicitly in your ``INSTALLED_APPS`` setting and enable in your
|
|
|
|
template with the ``{% load %}`` tag.
|
|
|
|
|
|
|
|
django.contrib.humanize
|
2008-11-25 06:01:24 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~
|
2006-06-04 08:58:39 +08:00
|
|
|
|
|
|
|
A set of Django template filters useful for adding a "human touch" to data. See
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`/ref/contrib/humanize`.
|
2006-06-04 08:58:39 +08:00
|
|
|
|
|
|
|
django.contrib.markup
|
2008-11-25 06:01:24 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2006-06-04 08:58:39 +08:00
|
|
|
|
|
|
|
A collection of template filters that implement these common markup languages:
|
|
|
|
|
|
|
|
* Textile
|
|
|
|
* Markdown
|
|
|
|
* ReST (ReStructured Text)
|
2007-04-16 19:26:07 +08:00
|
|
|
|
2010-08-28 10:40:17 +08:00
|
|
|
See the :doc:`markup documentation </ref/contrib/markup>`.
|
2007-09-15 17:51:57 +08:00
|
|
|
|
2007-04-16 19:26:07 +08:00
|
|
|
django.contrib.webdesign
|
2008-11-25 06:01:24 +08:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
2007-04-16 19:26:07 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
A collection of template tags that can be useful while designing a website,
|
2010-08-20 03:27:44 +08:00
|
|
|
such as a generator of Lorem Ipsum text. See :doc:`/ref/contrib/webdesign`.
|
2010-05-09 21:55:15 +08:00
|
|
|
|
|
|
|
i18n
|
|
|
|
~~~~
|
|
|
|
|
|
|
|
Provides a couple of templatetags that allow specifying translatable text in
|
|
|
|
Django templates. It is slightly different from the libraries described
|
|
|
|
above because you don't need to add any application to the ``INSTALLED_APPS``
|
|
|
|
setting but rather set :setting:`USE_I18N` to True, then loading it with
|
|
|
|
``{% load i18n %}``. See :ref:`specifying-translation-strings-in-template-code`.
|