2008-08-23 22:25:40 +00:00
|
|
|
==================================
|
|
|
|
Built-in template tags and filters
|
|
|
|
==================================
|
2007-11-30 05:18:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
This document describes Django's built-in template tags and filters. It is
|
2010-11-07 09:21:55 +00:00
|
|
|
recommended that you use the :doc:`automatic documentation
|
|
|
|
</ref/contrib/admin/admindocs>`, if available, as this will also include
|
2008-08-23 22:25:40 +00:00
|
|
|
documentation for any custom tags or filters installed.
|
2007-10-19 05:07:47 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. _ref-templates-builtins-tags:
|
2005-07-13 01:25:57 +00:00
|
|
|
|
|
|
|
Built-in tag reference
|
2016-01-03 10:56:22 +00:00
|
|
|
======================
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: autoescape
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``autoescape``
|
|
|
|
--------------
|
2007-11-14 12:58:53 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Controls the current auto-escaping behavior. This tag takes either ``on`` or
|
2007-11-14 12:58:53 +00:00
|
|
|
``off`` as an argument and that determines whether auto-escaping is in effect
|
2010-09-07 20:49:32 +00:00
|
|
|
inside the block. The block is closed with an ``endautoescape`` ending tag.
|
2007-11-14 12:58:53 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-09-07 20:49:32 +00:00
|
|
|
|
|
|
|
{% autoescape on %}
|
|
|
|
{{ body }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2023-05-24 16:59:00 +00:00
|
|
|
When auto-escaping is in effect, all content derived from variables has HTML
|
|
|
|
escaping applied before placing the result into the output (but after any
|
|
|
|
filters are applied). This is equivalent to manually applying the
|
|
|
|
:tfilter:`escape` filter to each variable.
|
|
|
|
|
|
|
|
The only exceptions are variables already marked as "safe" from escaping.
|
|
|
|
Variables could be marked as "safe" by the code which populated the variable,
|
|
|
|
by applying the :tfilter:`safe` or :tfilter:`escape` filters, or because it's
|
|
|
|
the result of a previous filter that marked the string as "safe".
|
|
|
|
|
|
|
|
Within the scope of disabled auto-escaping, chaining filters, including
|
|
|
|
:tfilter:`escape`, may cause unexpected (but documented) results such as the
|
|
|
|
following:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
{{ my_list|join:", "|escape }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
|
|
|
The above code will output the joined elements of ``my_list`` unescaped. This
|
|
|
|
is because the filter chaining sequence executes first :tfilter:`join` on
|
|
|
|
``my_list`` (without applying escaping to each item since ``autoescape`` is
|
|
|
|
``off``), marking the result as safe. Subsequently, this safe result will be
|
|
|
|
fed to :tfilter:`escape` filter, which does not apply a second round of
|
|
|
|
escaping.
|
|
|
|
|
2023-05-24 16:59:56 +00:00
|
|
|
In order to properly escape every element in a sequence, use the
|
|
|
|
:tfilter:`escapeseq` filter:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
{{ my_list|escapeseq|join:", " }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: block
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``block``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Defines a block that can be overridden by child templates. See
|
2008-08-23 22:25:40 +00:00
|
|
|
:ref:`Template inheritance <template-inheritance>` for more information.
|
|
|
|
|
|
|
|
.. templatetag:: comment
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``comment``
|
|
|
|
-----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2012-03-05 03:25:27 +00:00
|
|
|
Ignores everything between ``{% comment %}`` and ``{% endcomment %}``.
|
2014-06-02 20:19:12 +00:00
|
|
|
An optional note may be inserted in the first tag. For example, this is
|
|
|
|
useful when commenting out code for documenting why the code was disabled.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
|
|
|
<p>Rendered text with {{ pub_date|date:"c" }}</p>
|
2014-06-02 20:19:12 +00:00
|
|
|
{% comment "Optional note" %}
|
2012-11-09 00:32:16 +00:00
|
|
|
<p>Commented out text with {{ create_date|date:"c" }}</p>
|
|
|
|
{% endcomment %}
|
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
``comment`` tags cannot be nested.
|
|
|
|
|
2009-11-23 13:44:24 +00:00
|
|
|
.. templatetag:: csrf_token
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``csrf_token``
|
|
|
|
--------------
|
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-26 23:23:07 +00:00
|
|
|
|
2012-06-07 13:02:35 +00:00
|
|
|
This tag is used for CSRF protection, as described in the documentation for
|
2014-10-31 22:39:46 +00:00
|
|
|
:doc:`Cross Site Request Forgeries </ref/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-26 23:23:07 +00:00
|
|
|
|
2009-11-23 13:44:24 +00:00
|
|
|
.. templatetag:: cycle
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``cycle``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Produces one of its arguments each time this tag is encountered. The first
|
|
|
|
argument is produced on the first encounter, the second argument on the second
|
|
|
|
encounter, and so forth. Once all arguments are exhausted, the tag cycles to
|
|
|
|
the first argument and produces it again.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
This tag is particularly useful in a loop:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
{% for o in some_list %}
|
2008-09-30 17:45:58 +00:00
|
|
|
<tr class="{% cycle 'row1' 'row2' %}">
|
2005-11-06 23:30:06 +00:00
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
2007-10-20 11:05:15 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
The first iteration produces HTML that refers to class ``row1``, the second to
|
|
|
|
``row2``, the third to ``row1`` again, and so on for each iteration of the
|
|
|
|
loop.
|
|
|
|
|
2008-09-30 17:45:58 +00:00
|
|
|
You can use variables, too. For example, if you have two template variables,
|
2013-08-21 17:53:38 +00:00
|
|
|
``rowvalue1`` and ``rowvalue2``, you can alternate between their values like
|
2023-02-09 15:48:46 +00:00
|
|
|
this:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2008-09-30 17:45:58 +00:00
|
|
|
{% for o in some_list %}
|
|
|
|
<tr class="{% cycle rowvalue1 rowvalue2 %}">
|
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
|
2014-03-21 17:17:10 +00:00
|
|
|
Variables included in the cycle will be escaped. You can disable auto-escaping
|
2023-02-09 15:48:46 +00:00
|
|
|
with:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-12-09 19:53:06 +00:00
|
|
|
|
|
|
|
{% for o in some_list %}
|
2014-08-19 00:37:16 +00:00
|
|
|
<tr class="{% autoescape off %}{% cycle rowvalue1 rowvalue2 %}{% endautoescape %}">
|
2011-12-09 19:53:06 +00:00
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can mix variables and strings:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-09-30 17:45:58 +00:00
|
|
|
|
|
|
|
{% for o in some_list %}
|
|
|
|
<tr class="{% cycle 'row1' rowvalue2 'row3' %}">
|
|
|
|
...
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
In some cases you might want to refer to the current value of a cycle
|
|
|
|
without advancing to the next value. To do this,
|
2023-02-09 15:48:46 +00:00
|
|
|
give the ``{% cycle %}`` tag a name, using "as", like this:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-09-30 17:45:58 +00:00
|
|
|
|
|
|
|
{% cycle 'row1' 'row2' as rowcolors %}
|
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
From then on, you can insert the current value of the cycle wherever you'd like
|
|
|
|
in your template by referencing the cycle name as a context variable. If you
|
|
|
|
want to move the cycle to the next value independently of the original
|
|
|
|
``cycle`` tag, you can use another ``cycle`` tag and specify the name of the
|
2023-02-09 15:48:46 +00:00
|
|
|
variable. So, the following template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-03-08 13:43:53 +00:00
|
|
|
|
|
|
|
<tr>
|
|
|
|
<td class="{% cycle 'row1' 'row2' as rowcolors %}">...</td>
|
|
|
|
<td class="{{ rowcolors }}">...</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td class="{% cycle rowcolors %}">...</td>
|
|
|
|
<td class="{{ rowcolors }}">...</td>
|
|
|
|
</tr>
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
would output:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-03-08 13:43:53 +00:00
|
|
|
|
|
|
|
<tr>
|
|
|
|
<td class="row1">...</td>
|
|
|
|
<td class="row1">...</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td class="row2">...</td>
|
|
|
|
<td class="row2">...</td>
|
|
|
|
</tr>
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
You can use any number of values in a ``cycle`` tag, separated by spaces.
|
|
|
|
Values enclosed in single quotes (``'``) or double quotes (``"``) are treated
|
|
|
|
as string literals, while values without quotes are treated as template
|
|
|
|
variables.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2010-11-02 13:42:30 +00:00
|
|
|
By default, when you use the ``as`` keyword with the cycle tag, the
|
2013-08-21 17:53:38 +00:00
|
|
|
usage of ``{% cycle %}`` that initiates the cycle will itself produce
|
2010-11-02 13:42:30 +00:00
|
|
|
the first value in the cycle. This could be a problem if you want to
|
2013-08-21 17:53:38 +00:00
|
|
|
use the value in a nested loop or an included template. If you only want
|
|
|
|
to declare the cycle but not produce the first value, you can add a
|
2023-02-09 15:48:46 +00:00
|
|
|
``silent`` keyword as the last keyword in the tag. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-11-02 13:42:30 +00:00
|
|
|
|
|
|
|
{% for obj in some_list %}
|
2011-03-08 13:43:53 +00:00
|
|
|
{% cycle 'row1' 'row2' as rowcolors silent %}
|
2013-04-05 05:52:12 +00:00
|
|
|
<tr class="{{ rowcolors }}">{% include "subtemplate.html" %}</tr>
|
2010-11-02 13:42:30 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
This will output a list of ``<tr>`` elements with ``class``
|
2013-08-21 17:53:38 +00:00
|
|
|
alternating between ``row1`` and ``row2``. The subtemplate will have
|
|
|
|
access to ``rowcolors`` in its context and the value will match the class
|
|
|
|
of the ``<tr>`` that encloses it. If the ``silent`` keyword were to be
|
|
|
|
omitted, ``row1`` and ``row2`` would be emitted as normal text, outside the
|
2011-03-08 13:43:53 +00:00
|
|
|
``<tr>`` element.
|
|
|
|
|
|
|
|
When the silent keyword is used on a cycle definition, the silence
|
2013-08-21 17:53:38 +00:00
|
|
|
automatically applies to all subsequent uses of that specific cycle tag.
|
|
|
|
The following template would output *nothing*, even though the second
|
2023-02-09 15:48:46 +00:00
|
|
|
call to ``{% cycle %}`` doesn't specify ``silent``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-03-08 13:43:53 +00:00
|
|
|
|
|
|
|
{% cycle 'row1' 'row2' as rowcolors silent %}
|
|
|
|
{% cycle rowcolors %}
|
2010-11-02 13:42:30 +00:00
|
|
|
|
2016-07-03 14:19:06 +00:00
|
|
|
You can use the :ttag:`resetcycle` tag to make a ``{% cycle %}`` tag restart
|
|
|
|
from its first value when it's next encountered.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: debug
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``debug``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Outputs a whole load of debugging information, including the current context
|
2022-01-01 23:37:40 +00:00
|
|
|
and imported modules. ``{% debug %}`` outputs nothing when the :setting:`DEBUG`
|
|
|
|
setting is ``False``.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.2.27
|
|
|
|
|
|
|
|
In older versions, debugging information was displayed when the
|
|
|
|
:setting:`DEBUG` setting was ``False``.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: extends
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``extends``
|
|
|
|
-----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Signals that this template extends a parent template.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2006-08-21 18:27:33 +00:00
|
|
|
This tag can be used in two ways:
|
2006-07-27 23:23:55 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``{% extends "base.html" %}`` (with quotes) uses the literal value
|
|
|
|
``"base.html"`` as the name of the parent template to extend.
|
2006-07-27 23:23:55 +00:00
|
|
|
|
2011-10-14 00:12:01 +00: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 15:15:26 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
See :ref:`template-inheritance` for more information.
|
|
|
|
|
2017-06-03 12:11:04 +00:00
|
|
|
Normally the template name is relative to the template loader's root directory.
|
|
|
|
A string argument may also be a relative path starting with ``./`` or ``../``.
|
2023-02-09 15:48:46 +00:00
|
|
|
For example, assume the following directory structure:
|
|
|
|
|
|
|
|
.. code-block:: text
|
2016-03-24 08:39:37 +00:00
|
|
|
|
|
|
|
dir1/
|
|
|
|
template.html
|
|
|
|
base2.html
|
|
|
|
my/
|
|
|
|
base3.html
|
|
|
|
base1.html
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
In ``template.html``, the following paths would be valid:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-03-24 08:39:37 +00:00
|
|
|
|
|
|
|
{% extends "./base2.html" %}
|
|
|
|
{% extends "../base1.html" %}
|
|
|
|
{% extends "./my/base3.html" %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: filter
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``filter``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Filters the contents of the block through one or more filters. Multiple
|
|
|
|
filters can be specified with pipes and filters can have arguments, just as
|
|
|
|
in variable syntax.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Note that the block includes *all* the text between the ``filter`` and
|
|
|
|
``endfilter`` tags.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-11-14 12:58:53 +00:00
|
|
|
{% filter force_escape|lower %}
|
2005-11-06 23:30:06 +00:00
|
|
|
This text will be HTML-escaped, and will appear in all lowercase.
|
|
|
|
{% endfilter %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-12-24 20:45:58 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The :tfilter:`escape` and :tfilter:`safe` filters are not acceptable
|
|
|
|
arguments. Instead, use the :ttag:`autoescape` tag to manage autoescaping
|
|
|
|
for blocks of template code.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: firstof
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``firstof``
|
|
|
|
-----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2019-12-02 15:23:42 +00:00
|
|
|
Outputs the first argument variable that is not "false" (i.e. exists, is not
|
|
|
|
empty, is not a false boolean value, and is not a zero numeric value). Outputs
|
|
|
|
nothing if all the passed variables are "false".
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
{% firstof var1 var2 var3 %}
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
This is equivalent to:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
{% if var1 %}
|
2014-10-21 12:07:32 +00:00
|
|
|
{{ var1 }}
|
2013-02-23 14:07:21 +00:00
|
|
|
{% elif var2 %}
|
2014-10-21 12:07:32 +00:00
|
|
|
{{ var2 }}
|
2013-02-23 14:07:21 +00:00
|
|
|
{% elif var3 %}
|
2014-10-21 12:07:32 +00:00
|
|
|
{{ var3 }}
|
2013-02-23 14:07:21 +00:00
|
|
|
{% endif %}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2007-10-20 15:01:31 +00:00
|
|
|
You can also use a literal string as a fallback value in case all
|
2023-02-09 15:48:46 +00:00
|
|
|
passed variables are False:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-10-20 15:01:31 +00:00
|
|
|
|
|
|
|
{% firstof var1 var2 var3 "fallback value" %}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
This tag auto-escapes variable values. You can disable auto-escaping with:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-02-23 14:07:21 +00:00
|
|
|
|
2014-03-21 17:17:10 +00:00
|
|
|
{% autoescape off %}
|
|
|
|
{% firstof var1 var2 var3 "<strong>fallback value</strong>" %}
|
|
|
|
{% endautoescape %}
|
2013-02-23 14:07:21 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Or if only some variables should be escaped, you can use:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-02-23 14:07:21 +00:00
|
|
|
|
2014-03-21 17:17:10 +00:00
|
|
|
{% firstof var1 var2|safe var3 "<strong>fallback value</strong>"|safe %}
|
2013-02-23 14:07:21 +00:00
|
|
|
|
2015-04-13 15:27:30 +00:00
|
|
|
You can use the syntax ``{% firstof var1 var2 var3 as value %}`` to store the
|
|
|
|
output inside a variable.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: for
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``for``
|
|
|
|
-------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Loops over each item in an array, making the item available in a context
|
|
|
|
variable. For example, to display a list of athletes provided in
|
2023-02-09 15:48:46 +00:00
|
|
|
``athlete_list``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
You can loop over a list in reverse by using
|
|
|
|
``{% for obj in list reversed %}``.
|
2007-06-08 11:58:03 +00:00
|
|
|
|
|
|
|
If you need to loop over a list of lists, you can unpack the values
|
2013-08-21 17:53:38 +00:00
|
|
|
in each sublist into individual variables. For example, if your context
|
2008-07-30 12:37:38 +00:00
|
|
|
contains a list of (x,y) coordinates called ``points``, you could use the
|
2023-02-09 15:48:46 +00:00
|
|
|
following to output the list of points:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-06-08 11:58:03 +00:00
|
|
|
|
|
|
|
{% for x, y in points %}
|
|
|
|
There is a point at {{ x }},{{ y }}
|
|
|
|
{% endfor %}
|
2007-10-20 11:05:15 +00:00
|
|
|
|
|
|
|
This can also be useful if you need to access the items in a dictionary.
|
2007-06-08 11:58:03 +00:00
|
|
|
For example, if your context contained a dictionary ``data``, the following
|
2023-02-09 15:48:46 +00:00
|
|
|
would display the keys and values of the dictionary:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-06-08 11:58:03 +00:00
|
|
|
|
|
|
|
{% for key, value in data.items %}
|
|
|
|
{{ key }}: {{ value }}
|
|
|
|
{% endfor %}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2015-10-20 17:12:48 +00:00
|
|
|
Keep in mind that for the dot operator, dictionary key lookup takes precedence
|
|
|
|
over method lookup. Therefore if the ``data`` dictionary contains a key named
|
|
|
|
``'items'``, ``data.items`` will return ``data['items']`` instead of
|
|
|
|
``data.items()``. Avoid adding keys that are named like dictionary methods if
|
|
|
|
you want to use those methods in a template (``items``, ``values``, ``keys``,
|
|
|
|
etc.). Read more about the lookup order of the dot operator in the
|
|
|
|
:ref:`documentation of template variables <template-variables>`.
|
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
The for loop sets a number of variables available within the loop:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
========================== ===============================================
|
|
|
|
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
|
2013-08-21 17:53:38 +00:00
|
|
|
``forloop.parentloop`` For nested loops, this is the loop surrounding
|
|
|
|
the current one
|
2011-10-14 00:12:01 +00:00
|
|
|
========================== ===============================================
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``for`` ... ``empty``
|
|
|
|
---------------------
|
2008-11-24 22:01:48 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
The ``for`` tag can take an optional ``{% empty %}`` clause whose text is
|
2023-02-09 15:48:46 +00:00
|
|
|
displayed if the given array is empty or could not be found:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-11-24 22:01:48 +00:00
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% empty %}
|
2013-06-15 10:02:25 +00:00
|
|
|
<li>Sorry, no athletes in this list.</li>
|
2008-11-24 22:01:48 +00:00
|
|
|
{% endfor %}
|
2013-11-19 17:53:28 +00:00
|
|
|
</ul>
|
2008-11-24 22:01:48 +00:00
|
|
|
|
|
|
|
The above is equivalent to -- but shorter, cleaner, and possibly faster
|
2023-02-09 15:48:46 +00:00
|
|
|
than -- the following:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-11-24 22:01:48 +00:00
|
|
|
|
|
|
|
<ul>
|
2008-12-03 05:50:46 +00:00
|
|
|
{% if athlete_list %}
|
2008-11-24 22:01:48 +00:00
|
|
|
{% for athlete in athlete_list %}
|
|
|
|
<li>{{ athlete.name }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
{% else %}
|
|
|
|
<li>Sorry, no athletes in this list.</li>
|
|
|
|
{% endif %}
|
|
|
|
</ul>
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: if
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``if``
|
|
|
|
------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
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
|
2023-02-09 15:48:46 +00:00
|
|
|
block are output:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
{% if athlete_list %}
|
|
|
|
Number of athletes: {{ athlete_list|length }}
|
2011-12-09 22:13:27 +00:00
|
|
|
{% elif athlete_in_locker_room_list %}
|
|
|
|
Athletes should be out of the locker room soon!
|
2005-11-06 23:30:06 +00:00
|
|
|
{% 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.
|
|
|
|
|
2013-01-01 13:12:42 +00:00
|
|
|
As you can see, the ``if`` tag may take one or several ``{% elif %}``
|
2011-12-09 22:13:27 +00:00
|
|
|
clauses, as well as an ``{% else %}`` clause that will be displayed if all
|
|
|
|
previous conditions fail. These clauses are optional.
|
|
|
|
|
2009-12-09 22:40:36 +00:00
|
|
|
Boolean operators
|
2016-01-03 10:56:22 +00:00
|
|
|
~~~~~~~~~~~~~~~~~
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
:ttag:`if` tags may use ``and``, ``or`` or ``not`` to test a number of
|
2023-02-09 15:48:46 +00:00
|
|
|
variables or to negate a given variable:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-06-08 03:33:21 +00:00
|
|
|
|
|
|
|
{% if athlete_list and coach_list %}
|
|
|
|
Both athletes and coaches are available.
|
|
|
|
{% endif %}
|
2005-11-06 23:30:06 +00: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 %}
|
2014-09-08 19:16:59 +00:00
|
|
|
There are no athletes or there are some coaches.
|
2006-06-08 03:33:21 +00:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if athlete_list and not coach_list %}
|
|
|
|
There are some athletes and absolutely no coaches.
|
2005-11-06 23:30:06 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2009-12-09 22:40:36 +00:00
|
|
|
Use of both ``and`` and ``or`` clauses within the same tag is allowed, with
|
2023-02-09 15:48:46 +00:00
|
|
|
``and`` having higher precedence than ``or`` e.g.:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-06-08 03:33:21 +00:00
|
|
|
|
|
|
|
{% if athlete_list and coach_list or cheerleader_list %}
|
|
|
|
|
2023-12-28 18:52:15 +00:00
|
|
|
will be interpreted like::
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-12-28 18:52:15 +00:00
|
|
|
if (athlete_list and coach_list) or cheerleader_list:
|
|
|
|
...
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Use of actual parentheses in the :ttag:`if` tag is invalid syntax. If you need
|
2011-10-03 08:06:01 +00:00
|
|
|
them to indicate precedence, you should use nested :ttag:`if` tags.
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
:ttag:`if` tags may also use the operators ``==``, ``!=``, ``<``, ``>``,
|
2016-04-09 15:25:00 +00:00
|
|
|
``<=``, ``>=``, ``in``, ``not in``, ``is``, and ``is not`` which work as
|
|
|
|
follows:
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
``==`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Equality. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if somevar == "x" %}
|
|
|
|
This appears if variable somevar equals the string "x"
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``!=`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Inequality. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% 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
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Less than. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if somevar < 100 %}
|
|
|
|
This appears if variable somevar is less than 100.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``>`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Greater than. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if somevar > 0 %}
|
|
|
|
This appears if variable somevar is greater than 0.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``<=`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Less than or equal to. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if somevar <= 100 %}
|
|
|
|
This appears if variable somevar is less than 100 or equal to 100.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``>=`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Greater than or equal to. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if somevar >= 1 %}
|
|
|
|
This appears if variable somevar is greater than 1 or equal to 1.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
``in`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
Contained within. This operator is supported by many Python containers to test
|
2013-08-21 17:53:38 +00:00
|
|
|
whether the given value is in the container. The following are some examples
|
2023-02-09 15:48:46 +00:00
|
|
|
of how ``x in y`` will be interpreted:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% 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-08 23:25:37 +00:00
|
|
|
``not in`` operator
|
2016-01-03 10:56:22 +00:00
|
|
|
^^^^^^^^^^^^^^^^^^^
|
2010-03-08 23:25:37 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Not contained within. This is the negation of the ``in`` operator.
|
2010-03-08 23:25:37 +00:00
|
|
|
|
2016-01-22 03:50:06 +00:00
|
|
|
``is`` operator
|
|
|
|
^^^^^^^^^^^^^^^
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Object identity. Tests if two values are the same object. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-01-22 03:50:06 +00:00
|
|
|
|
2016-04-28 21:51:40 +00:00
|
|
|
{% if somevar is True %}
|
|
|
|
This appears if and only if somevar is True.
|
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
{% if somevar is None %}
|
|
|
|
This appears if somevar is None, or if somevar is not found in the context.
|
2016-01-22 03:50:06 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2016-04-09 15:25:00 +00:00
|
|
|
``is not`` operator
|
|
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2016-04-28 21:51:40 +00:00
|
|
|
Negated object identity. Tests if two values are not the same object. This is
|
2023-02-09 15:48:46 +00:00
|
|
|
the negation of the ``is`` operator. Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-04-28 21:51:40 +00:00
|
|
|
|
|
|
|
{% if somevar is not True %}
|
|
|
|
This appears if somevar is not True, or if somevar is not found in the
|
|
|
|
context.
|
|
|
|
{% endif %}
|
2016-04-09 15:25:00 +00:00
|
|
|
|
2016-04-28 21:51:40 +00:00
|
|
|
{% if somevar is not None %}
|
|
|
|
This appears if and only if somevar is not None.
|
2016-04-09 15:25:00 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2009-12-09 22:40:36 +00:00
|
|
|
Filters
|
2016-01-03 10:56:22 +00:00
|
|
|
~~~~~~~
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can also use filters in the :ttag:`if` expression. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
{% if messages|length >= 100 %}
|
|
|
|
You have lots of messages today!
|
2005-11-06 23:30:06 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2009-12-09 22:40:36 +00:00
|
|
|
Complex expressions
|
2016-01-03 10:56:22 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~
|
2009-12-09 22:40:36 +00:00
|
|
|
|
|
|
|
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
|
2013-08-21 17:53:38 +00:00
|
|
|
expression is evaluated - that is, the precedence rules. The precedence of the
|
2009-12-09 22:40:36 +00:00
|
|
|
operators, from lowest to highest, is as follows:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``or``
|
|
|
|
* ``and``
|
|
|
|
* ``not``
|
|
|
|
* ``in``
|
|
|
|
* ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=``
|
2009-12-09 22:40:36 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
(This follows Python exactly). So, for example, the following complex
|
2023-02-09 15:48:46 +00:00
|
|
|
:ttag:`if` tag:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-05-22 16:43:50 +00:00
|
|
|
|
2009-12-09 22:40:36 +00:00
|
|
|
{% if a == b or c == d and e %}
|
|
|
|
|
|
|
|
...will be interpreted as:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
(a == b) or ((c == d) and e)
|
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If you need different precedence, you will need to use nested :ttag:`if` tags.
|
|
|
|
Sometimes that is better for clarity anyway, for the sake of those who do not
|
|
|
|
know the precedence rules.
|
2006-07-29 00:31:33 +00:00
|
|
|
|
2016-04-09 15:28:07 +00:00
|
|
|
The comparison operators cannot be 'chained' like in Python or in mathematical
|
2023-02-09 15:48:46 +00:00
|
|
|
notation. For example, instead of using:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-04-09 15:28:07 +00:00
|
|
|
|
|
|
|
{% if a > b > c %} (WRONG)
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
you should use:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-04-09 15:28:07 +00:00
|
|
|
|
|
|
|
{% if a > b and b > c %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: ifchanged
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``ifchanged``
|
|
|
|
-------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Check if a value has changed from the last iteration of a loop.
|
|
|
|
|
2011-09-25 05:21:29 +00:00
|
|
|
The ``{% ifchanged %}`` block tag is used within a loop. It has two possible
|
|
|
|
uses.
|
2006-11-07 05:36:51 +00:00
|
|
|
|
|
|
|
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
|
2023-02-09 15:48:46 +00:00
|
|
|
days, only displaying the month if it changes:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-11-07 05:36:51 +00:00
|
|
|
|
|
|
|
<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-07 23:42:27 +00:00
|
|
|
|
2011-09-25 05:21:29 +00:00
|
|
|
2. If given one or more variables, check whether any variable has changed.
|
|
|
|
For example, the following shows the date every time it changes, while
|
2023-02-09 15:48:46 +00:00
|
|
|
showing the hour if either the hour or the date has changed:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-11-07 23:42:27 +00:00
|
|
|
|
2006-11-07 05:36:51 +00:00
|
|
|
{% for date in days %}
|
2006-11-07 19:07:27 +00:00
|
|
|
{% ifchanged date.date %} {{ date.date }} {% endifchanged %}
|
2006-11-07 05:36:51 +00:00
|
|
|
{% ifchanged date.hour date.date %}
|
2006-11-07 19:07:27 +00:00
|
|
|
{{ date.hour }}
|
2006-11-07 05:36:51 +00:00
|
|
|
{% endifchanged %}
|
|
|
|
{% endfor %}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2009-04-16 12:46:15 +00:00
|
|
|
The ``ifchanged`` tag can also take an optional ``{% else %}`` clause that
|
2023-02-09 15:48:46 +00:00
|
|
|
will be displayed if the value has not changed:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-04-16 12:46:15 +00:00
|
|
|
|
|
|
|
{% for match in matches %}
|
|
|
|
<div style="background-color:
|
|
|
|
{% ifchanged match.ballot_id %}
|
2009-05-14 01:53:57 +00:00
|
|
|
{% cycle "red" "blue" %}
|
2009-04-16 12:46:15 +00:00
|
|
|
{% else %}
|
2014-03-02 15:00:30 +00:00
|
|
|
gray
|
2009-04-16 12:46:15 +00:00
|
|
|
{% endifchanged %}
|
|
|
|
">{{ match }}</div>
|
|
|
|
{% endfor %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: include
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``include``
|
|
|
|
-----------
|
2005-11-22 05:44:04 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
This example includes the contents of the template ``"foo/bar.html"``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
{% include "foo/bar.html" %}
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2017-06-03 12:11:04 +00:00
|
|
|
Normally the template name is relative to the template loader's root directory.
|
|
|
|
A string argument may also be a relative path starting with ``./`` or ``../``
|
|
|
|
as described in the :ttag:`extends` tag.
|
2016-03-24 08:39:37 +00:00
|
|
|
|
2005-11-22 05:44:04 +00:00
|
|
|
This example includes the contents of the template whose name is contained in
|
2023-02-09 15:48:46 +00:00
|
|
|
the variable ``template_name``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-22 05:44:04 +00:00
|
|
|
|
|
|
|
{% include template_name %}
|
|
|
|
|
2015-01-26 20:39:52 +00:00
|
|
|
The variable may also be any object with a ``render()`` method that accepts a
|
|
|
|
context. This allows you to reference a compiled ``Template`` in your context.
|
2013-08-28 12:17:20 +00:00
|
|
|
|
2020-02-02 16:48:07 +00:00
|
|
|
Additionally, the variable may be an iterable of template names, in which case
|
|
|
|
the first that can be loaded will be used, as per
|
|
|
|
:func:`~django.template.loader.select_template`.
|
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
An included template is rendered within the context of the template that
|
2015-04-17 11:51:35 +00:00
|
|
|
includes it. This example produces the output ``"Hello, John!"``:
|
|
|
|
|
|
|
|
* Context: variable ``person`` is set to ``"John"`` and variable ``greeting``
|
|
|
|
is set to ``"Hello"``.
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
* Template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
{% include "name_snippet.html" %}
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
* The ``name_snippet.html`` template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
{{ greeting }}, {{ person|default:"friend" }}!
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can pass additional context to the template using keyword arguments:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-12-18 02:50:26 +00:00
|
|
|
|
2011-04-18 23:15:28 +00:00
|
|
|
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
|
2010-12-18 02:50:26 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
If you want to render the context only with the variables provided (or even
|
|
|
|
no variables at all), use the ``only`` option. No other variables are
|
2023-02-09 15:48:46 +00:00
|
|
|
available to the included template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-12-18 02:50:26 +00:00
|
|
|
|
|
|
|
{% include "name_snippet.html" with greeting="Hi" only %}
|
2005-11-22 05:44:04 +00:00
|
|
|
|
2010-05-01 14:27:10 +00: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.
|
|
|
|
|
2014-10-14 12:48:45 +00:00
|
|
|
Blocks are evaluated *before* they are included. This means that a template
|
|
|
|
that includes blocks from another will contain blocks that have *already
|
|
|
|
been evaluated and rendered* - not blocks that can be overridden by, for
|
|
|
|
example, an extending template.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: load
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``load``
|
|
|
|
--------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Loads a custom template tag set.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-03-03 19:31:10 +00:00
|
|
|
For example, the following template would load all the tags and filters
|
2011-11-21 10:28:12 +00:00
|
|
|
registered in ``somelibrary`` and ``otherlibrary`` located in package
|
2023-02-09 15:48:46 +00:00
|
|
|
``package``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-11-20 06:22:28 +00:00
|
|
|
|
2011-11-21 10:28:12 +00:00
|
|
|
{% load somelibrary package.otherlibrary %}
|
2010-11-20 06:22:28 +00:00
|
|
|
|
2010-11-20 11:16:43 +00:00
|
|
|
You can also selectively load individual filters or tags from a library, using
|
2010-11-20 06:22:28 +00:00
|
|
|
the ``from`` argument. In this example, the template tags/filters named ``foo``
|
2023-02-09 15:48:46 +00:00
|
|
|
and ``bar`` will be loaded from ``somelibrary``:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-11-20 06:22:28 +00:00
|
|
|
|
|
|
|
{% load foo bar from somelibrary %}
|
|
|
|
|
2011-03-03 19:31:10 +00:00
|
|
|
See :doc:`Custom tag and filter libraries </howto/custom-template-tags>` for
|
|
|
|
more information.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2014-07-12 13:50:27 +00:00
|
|
|
.. templatetag:: lorem
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``lorem``
|
|
|
|
---------
|
2014-07-12 13:50:27 +00:00
|
|
|
|
|
|
|
Displays random "lorem ipsum" Latin text. This is useful for providing sample
|
|
|
|
data in templates.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2014-07-12 13:50:27 +00:00
|
|
|
|
|
|
|
{% lorem [count] [method] [random] %}
|
|
|
|
|
|
|
|
The ``{% lorem %}`` tag can be used with zero, one, two or three arguments.
|
|
|
|
The arguments are:
|
|
|
|
|
|
|
|
=========== =============================================================
|
|
|
|
Argument Description
|
|
|
|
=========== =============================================================
|
|
|
|
``count`` A number (or variable) containing the number of paragraphs or
|
|
|
|
words to generate (default is 1).
|
|
|
|
``method`` Either ``w`` for words, ``p`` for HTML paragraphs or ``b``
|
|
|
|
for plain-text paragraph blocks (default is ``b``).
|
|
|
|
``random`` The word ``random``, which if given, does not use the common
|
|
|
|
paragraph ("Lorem ipsum dolor sit amet...") when generating
|
|
|
|
text.
|
|
|
|
=========== =============================================================
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph.
|
|
|
|
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph
|
|
|
|
and two random paragraphs each wrapped in HTML ``<p>`` tags.
|
|
|
|
* ``{% lorem 2 w random %}`` will output two random Latin words.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: now
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``now``
|
|
|
|
-------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Displays the current date and/or time, using a format according to the given
|
2011-03-01 03:05:57 +00:00
|
|
|
string. Such string can contain format specifiers characters as described
|
|
|
|
in the :tfilter:`date` filter section.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
It is {% now "jS F Y H:i" %}
|
|
|
|
|
|
|
|
Note that you can backslash-escape a format string if you want to use the
|
2014-09-17 03:30:11 +00:00
|
|
|
"raw" value. In this example, both "o" and "f" are backslash-escaped, because
|
|
|
|
otherwise each is a format string that displays the year and the time,
|
2023-02-09 15:48:46 +00:00
|
|
|
respectively:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2014-09-17 03:30:11 +00:00
|
|
|
It is the {% now "jS \o\f F" %}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2007-04-12 11:12:45 +00:00
|
|
|
This would display as "It is the 4th of September".
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-05-07 16:58:45 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The format passed can also be one of the predefined ones
|
|
|
|
:setting:`DATE_FORMAT`, :setting:`DATETIME_FORMAT`,
|
|
|
|
:setting:`SHORT_DATE_FORMAT` or :setting:`SHORT_DATETIME_FORMAT`.
|
|
|
|
The predefined formats may vary depending on the current locale and
|
2023-02-09 15:48:46 +00:00
|
|
|
if :doc:`/topics/i18n/formatting` is enabled, e.g.:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-05-07 16:58:45 +00:00
|
|
|
|
|
|
|
It is {% now "SHORT_DATETIME_FORMAT" %}
|
|
|
|
|
2014-11-25 18:24:57 +00:00
|
|
|
You can also use the syntax ``{% now "Y" as current_year %}`` to store the
|
2015-07-04 12:46:49 +00:00
|
|
|
output (as a string) inside a variable. This is useful if you want to use
|
2023-02-09 15:48:46 +00:00
|
|
|
``{% now %}`` inside a template tag like :ttag:`blocktranslate` for example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2014-11-25 18:24:57 +00:00
|
|
|
|
|
|
|
{% now "Y" as current_year %}
|
2019-06-21 16:41:01 +00:00
|
|
|
{% blocktranslate %}Copyright {{ current_year }}{% endblocktranslate %}
|
2014-11-25 18:24:57 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: regroup
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``regroup``
|
|
|
|
-----------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Regroups a list of alike objects by a common attribute.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2016-08-26 09:37:20 +00:00
|
|
|
This complex tag is best illustrated by way of an example: say that ``cities``
|
|
|
|
is a list of cities represented by dictionaries containing ``"name"``,
|
2015-08-08 12:09:27 +00:00
|
|
|
``"population"``, and ``"country"`` keys:
|
2008-09-09 01:54:20 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
cities = [
|
2023-02-28 19:53:28 +00:00
|
|
|
{"name": "Mumbai", "population": "19,000,000", "country": "India"},
|
|
|
|
{"name": "Calcutta", "population": "15,000,000", "country": "India"},
|
|
|
|
{"name": "New York", "population": "20,000,000", "country": "USA"},
|
|
|
|
{"name": "Chicago", "population": "7,000,000", "country": "USA"},
|
|
|
|
{"name": "Tokyo", "population": "33,000,000", "country": "Japan"},
|
2007-07-15 21:16:32 +00:00
|
|
|
]
|
|
|
|
|
2015-08-08 12:09:27 +00:00
|
|
|
...and you'd like to display a hierarchical list that is ordered by country,
|
|
|
|
like this:
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* India
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Mumbai: 19,000,000
|
|
|
|
* Calcutta: 15,000,000
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* USA
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* New York: 20,000,000
|
|
|
|
* Chicago: 7,000,000
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* Japan
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Tokyo: 33,000,000
|
2011-11-17 22:00:08 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
You can use the ``{% regroup %}`` tag to group the list of cities by country.
|
2023-02-09 15:48:46 +00:00
|
|
|
The following snippet of template code would accomplish this:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
{% regroup cities by country as country_list %}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
<ul>
|
2012-06-06 09:54:26 +00:00
|
|
|
{% for country in country_list %}
|
|
|
|
<li>{{ country.grouper }}
|
2005-07-15 00:42:28 +00:00
|
|
|
<ul>
|
2016-08-26 09:37:20 +00:00
|
|
|
{% for city in country.list %}
|
|
|
|
<li>{{ city.name }}: {{ city.population }}</li>
|
2005-11-06 23:30:06 +00:00
|
|
|
{% endfor %}
|
2005-07-15 00:42:28 +00:00
|
|
|
</ul>
|
2007-03-27 22:30:59 +00:00
|
|
|
</li>
|
2005-11-06 23:30:06 +00:00
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-07-15 21:16:32 +00: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
|
2012-06-06 09:54:26 +00:00
|
|
|
resulting list. Here, we're regrouping the ``cities`` list by the ``country``
|
|
|
|
attribute and calling the result ``country_list``.
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
``{% regroup %}`` produces a list (in this case, ``country_list``) of
|
2016-08-26 09:39:06 +00:00
|
|
|
**group objects**. Group objects are instances of
|
|
|
|
:py:func:`~collections.namedtuple` with two fields:
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* ``grouper`` -- the item that was grouped by (e.g., the string "India" or
|
|
|
|
"Japan").
|
|
|
|
* ``list`` -- a list of all items in this group (e.g., a list of all cities
|
|
|
|
with country='India').
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2016-08-26 09:39:06 +00:00
|
|
|
Because ``{% regroup %}`` produces :py:func:`~collections.namedtuple` objects,
|
2023-02-09 15:48:46 +00:00
|
|
|
you can also write the previous example as:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-08-26 09:39:06 +00:00
|
|
|
|
|
|
|
{% regroup cities by country as country_list %}
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for country, local_cities in country_list %}
|
|
|
|
<li>{{ country }}
|
|
|
|
<ul>
|
|
|
|
{% for city in local_cities %}
|
|
|
|
<li>{{ city.name }}: {{ city.population }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
2007-07-15 21:16:32 +00:00
|
|
|
Note that ``{% regroup %}`` does not order its input! Our example relies on
|
2012-06-06 09:54:26 +00:00
|
|
|
the fact that the ``cities`` list was ordered by ``country`` in the first place.
|
|
|
|
If the ``cities`` list did *not* order its members by ``country``, the
|
|
|
|
regrouping would naively display more than one group for a single country. For
|
|
|
|
example, say the ``cities`` list was set to this (note that the countries are not
|
2011-10-03 08:06:01 +00:00
|
|
|
grouped together):
|
2008-09-09 01:54:20 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
2007-07-15 21:16:32 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
cities = [
|
2023-02-28 19:53:28 +00:00
|
|
|
{"name": "Mumbai", "population": "19,000,000", "country": "India"},
|
|
|
|
{"name": "New York", "population": "20,000,000", "country": "USA"},
|
|
|
|
{"name": "Calcutta", "population": "15,000,000", "country": "India"},
|
|
|
|
{"name": "Chicago", "population": "7,000,000", "country": "USA"},
|
|
|
|
{"name": "Tokyo", "population": "33,000,000", "country": "Japan"},
|
2007-07-15 21:16:32 +00:00
|
|
|
]
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
With this input for ``cities``, the example ``{% regroup %}`` template code
|
2007-07-15 21:16:32 +00:00
|
|
|
above would result in the following output:
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* India
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Mumbai: 19,000,000
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* USA
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* New York: 20,000,000
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* India
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Calcutta: 15,000,000
|
|
|
|
|
2013-05-29 22:33:51 +00:00
|
|
|
* USA
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Chicago: 7,000,000
|
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
* Japan
|
2014-12-17 13:01:19 +00:00
|
|
|
|
|
|
|
* Tokyo: 33,000,000
|
2007-07-15 21:16:32 +00:00
|
|
|
|
|
|
|
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 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Another solution is to sort the data in the template using the
|
2023-02-09 15:48:46 +00:00
|
|
|
:tfilter:`dictsort` filter, if your data is in a list of dictionaries:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
{% regroup cities|dictsort:"country" by country as country_list %}
|
2010-12-29 20:20:53 +00:00
|
|
|
|
|
|
|
Grouping on other properties
|
2016-01-03 10:56:22 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-12-29 20:20:53 +00:00
|
|
|
|
|
|
|
Any valid template lookup is a legal grouping attribute for the regroup
|
|
|
|
tag, including methods, attributes, dictionary keys and list items. For
|
2012-06-06 09:54:26 +00:00
|
|
|
example, if the "country" field is a foreign key to a class with
|
2023-02-09 15:48:46 +00:00
|
|
|
an attribute "description," you could use:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-12-29 20:20:53 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
{% regroup cities by country.description as country_list %}
|
2010-12-29 20:20:53 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
Or, if ``country`` is a field with ``choices``, it will have a
|
2012-12-25 14:56:22 +00:00
|
|
|
:meth:`~django.db.models.Model.get_FOO_display` method available as an
|
2010-12-29 20:20:53 +00:00
|
|
|
attribute, allowing you to group on the display string rather than the
|
2023-02-09 15:48:46 +00:00
|
|
|
``choices`` key:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-12-29 20:20:53 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
{% regroup cities by get_country_display as country_list %}
|
2010-12-29 20:20:53 +00:00
|
|
|
|
2012-06-06 09:54:26 +00:00
|
|
|
``{{ country.grouper }}`` will now display the value fields from the
|
2010-12-29 20:20:53 +00:00
|
|
|
``choices`` set rather than the keys.
|
|
|
|
|
2016-07-03 14:19:06 +00:00
|
|
|
.. templatetag:: resetcycle
|
|
|
|
|
|
|
|
``resetcycle``
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Resets a previous `cycle`_ so that it restarts from its first item at its next
|
|
|
|
encounter. Without arguments, ``{% resetcycle %}`` will reset the last
|
|
|
|
``{% cycle %}`` defined in the template.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-07-03 14:19:06 +00:00
|
|
|
|
|
|
|
{% for coach in coach_list %}
|
|
|
|
<h1>{{ coach.name }}</h1>
|
|
|
|
{% for athlete in coach.athlete_set.all %}
|
|
|
|
<p class="{% cycle 'odd' 'even' %}">{{ athlete.name }}</p>
|
|
|
|
{% endfor %}
|
|
|
|
{% resetcycle %}
|
|
|
|
{% endfor %}
|
|
|
|
|
2023-05-04 11:56:36 +00:00
|
|
|
This example would return this HTML:
|
2016-07-03 14:19:06 +00:00
|
|
|
|
2023-01-23 20:29:05 +00:00
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<h1>Gareth</h1>
|
|
|
|
<p class="odd">Harry</p>
|
|
|
|
<p class="even">John</p>
|
|
|
|
<p class="odd">Nick</p>
|
2016-07-03 14:19:06 +00:00
|
|
|
|
2023-01-23 20:29:05 +00:00
|
|
|
<h1>John</h1>
|
|
|
|
<p class="odd">Andrea</p>
|
|
|
|
<p class="even">Melissa</p>
|
2016-07-03 14:19:06 +00:00
|
|
|
|
|
|
|
Notice how the first block ends with ``class="odd"`` and the new one starts
|
|
|
|
with ``class="odd"``. Without the ``{% resetcycle %}`` tag, the second block
|
|
|
|
would start with ``class="even"``.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can also reset named cycle tags:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2016-07-03 14:19:06 +00:00
|
|
|
|
|
|
|
{% for item in list %}
|
|
|
|
<p class="{% cycle 'odd' 'even' as stripe %} {% cycle 'major' 'minor' 'minor' 'minor' 'minor' as tick %}">
|
|
|
|
{{ item.data }}
|
|
|
|
</p>
|
|
|
|
{% ifchanged item.category %}
|
|
|
|
<h1>{{ item.category }}</h1>
|
|
|
|
{% if not forloop.first %}{% resetcycle tick %}{% endif %}
|
|
|
|
{% endifchanged %}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
In this example, we have both the alternating odd/even rows and a "major" row
|
|
|
|
every fifth row. Only the five-row cycle is reset when a category changes.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: spaceless
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``spaceless``
|
|
|
|
-------------
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2007-04-01 01:09:21 +00:00
|
|
|
Removes whitespace between HTML tags. This includes tab
|
2006-01-15 05:18:17 +00:00
|
|
|
characters and newlines.
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-01-15 01:51:30 +00:00
|
|
|
|
|
|
|
{% spaceless %}
|
|
|
|
<p>
|
|
|
|
<a href="foo/">Foo</a>
|
|
|
|
</p>
|
2006-01-16 21:44:10 +00:00
|
|
|
{% endspaceless %}
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
This example would return this HTML:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2007-04-01 01:09:21 +00:00
|
|
|
<p><a href="foo/">Foo</a></p>
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2007-04-01 01:09:21 +00:00
|
|
|
Only space between *tags* is removed -- not space between tags and text. In
|
2023-02-09 15:48:46 +00:00
|
|
|
this example, the space around ``Hello`` won't be stripped:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-01-15 01:51:30 +00:00
|
|
|
|
|
|
|
{% spaceless %}
|
|
|
|
<strong>
|
|
|
|
Hello
|
|
|
|
</strong>
|
2006-01-16 21:44:10 +00:00
|
|
|
{% endspaceless %}
|
2006-01-15 01:51:30 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: templatetag
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``templatetag``
|
|
|
|
---------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Outputs one of the syntax characters used to compose template tags.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2021-08-31 18:31:23 +00:00
|
|
|
The template system has no concept of "escaping" individual characters.
|
|
|
|
However, you can use the ``{% templatetag %}`` tag to display one of the
|
|
|
|
template tag character combinations.
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
The argument tells which template bit to output:
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
================== =======
|
|
|
|
Argument Outputs
|
|
|
|
================== =======
|
|
|
|
``openblock`` ``{%``
|
|
|
|
``closeblock`` ``%}``
|
|
|
|
``openvariable`` ``{{``
|
|
|
|
``closevariable`` ``}}``
|
|
|
|
``openbrace`` ``{``
|
|
|
|
``closebrace`` ``}``
|
|
|
|
``opencomment`` ``{#``
|
|
|
|
``closecomment`` ``#}``
|
|
|
|
================== =======
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
2021-08-31 18:31:23 +00:00
|
|
|
The {% templatetag openblock %} characters open a block.
|
|
|
|
|
|
|
|
See also the :ttag:`verbatim` tag for another way of including these
|
|
|
|
characters.
|
2012-11-09 00:32:16 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: url
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``url``
|
|
|
|
-------
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2010-11-07 01:42:55 +00:00
|
|
|
Returns an absolute path reference (a URL without the domain name) matching a
|
2015-09-05 15:54:28 +00:00
|
|
|
given view and optional parameters. Any special characters in the resulting
|
|
|
|
path will be encoded using :func:`~django.utils.encoding.iri_to_uri`.
|
2014-01-22 15:44:50 +00:00
|
|
|
|
|
|
|
This is a way to output links without violating the DRY principle by having to
|
2023-02-09 15:48:46 +00:00
|
|
|
hard-code URLs in your templates:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
{% url 'some-url-name' v1 v2 %}
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
The first argument is a :ref:`URL pattern name <naming-url-patterns>`. It can
|
|
|
|
be a quoted literal or any other context variable. Additional arguments are
|
|
|
|
optional and should be space-separated values that will be used as arguments in
|
|
|
|
the URL. The example above shows passing positional arguments. Alternatively
|
2023-02-09 15:48:46 +00:00
|
|
|
you may use keyword syntax:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-03-02 04:04:23 +00:00
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
{% url 'some-url-name' arg1=v1 arg2=v2 %}
|
2010-03-02 04:04:23 +00:00
|
|
|
|
2010-03-15 07:22:20 +00: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 04:24:58 +00:00
|
|
|
|
2007-05-26 09:43:32 +00: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 01:54:20 +00:00
|
|
|
``app_views.py``). The URLconf line might look like this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
path("client/<int:id>/", app_views.client, name="app-views-client")
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2007-02-13 06:13:06 +00:00
|
|
|
If this app's URLconf is included into the project's URLconf under a path
|
2008-09-09 01:54:20 +00:00
|
|
|
such as this:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
path("clients/", include("project_name.app_name.urls"))
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2023-05-07 12:51:27 +00:00
|
|
|
...then, in a template, you can create a link to this view like this:
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2023-01-23 20:29:05 +00:00
|
|
|
.. code-block:: html+django
|
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
{% url 'app-views-client' client.id %}
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2007-02-13 06:13:06 +00:00
|
|
|
The template tag will output the string ``/clients/client/123/``.
|
2007-02-13 04:24:58 +00:00
|
|
|
|
2008-08-29 19:28:03 +00:00
|
|
|
Note that if the URL you're reversing doesn't exist, you'll get an
|
2015-12-30 15:51:16 +00:00
|
|
|
:exc:`~django.urls.NoReverseMatch` exception raised, which will cause your
|
|
|
|
site to display an error page.
|
2008-08-29 19:28:03 +00:00
|
|
|
|
2008-09-09 01:54:20 +00:00
|
|
|
If you'd like to retrieve a URL without displaying it, you can use a slightly
|
2023-02-09 15:48:46 +00:00
|
|
|
different call:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-08-29 19:28:03 +00:00
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
{% url 'some-url-name' arg arg2 as the_url %}
|
2009-02-22 06:09:28 +00:00
|
|
|
|
2008-08-29 19:28:03 +00:00
|
|
|
<a href="{{ the_url }}">I'm linking to {{ the_url }}</a>
|
2009-02-22 06:09:28 +00:00
|
|
|
|
2014-01-17 19:50:47 +00:00
|
|
|
The scope of the variable created by the ``as var`` syntax is the
|
|
|
|
``{% block %}`` in which the ``{% url %}`` tag appears.
|
|
|
|
|
2008-08-29 19:28:03 +00:00
|
|
|
This ``{% url ... as var %}`` syntax will *not* cause an error if the view is
|
2023-02-09 15:48:46 +00:00
|
|
|
missing. In practice you'll use this to link to views that are optional:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-08-29 19:28:03 +00:00
|
|
|
|
2014-06-03 11:30:14 +00:00
|
|
|
{% url 'some-url-name' as the_url %}
|
2008-08-29 19:28:03 +00:00
|
|
|
{% if the_url %}
|
|
|
|
<a href="{{ the_url }}">Link to optional stuff</a>
|
|
|
|
{% endif %}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
If you'd like to retrieve a namespaced URL, specify the fully qualified name:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-07-16 16:16:13 +00:00
|
|
|
|
2012-04-24 19:55:52 +00:00
|
|
|
{% url 'myapp:view-name' %}
|
2009-07-16 16:16:13 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2012-11-24 21:04:17 +00:00
|
|
|
.. warning::
|
|
|
|
|
2016-10-20 17:29:04 +00:00
|
|
|
Don't forget to put quotes around the URL pattern ``name``, otherwise the
|
|
|
|
value will be interpreted as a context variable!
|
2012-11-24 21:04:17 +00:00
|
|
|
|
2012-06-07 07:59:14 +00:00
|
|
|
.. templatetag:: verbatim
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``verbatim``
|
|
|
|
------------
|
2012-06-07 07:59:14 +00:00
|
|
|
|
|
|
|
Stops the template engine from rendering the contents of this block tag.
|
|
|
|
|
2015-04-30 19:39:29 +00:00
|
|
|
A common use is to allow a JavaScript template layer that collides with
|
2023-02-09 15:48:46 +00:00
|
|
|
Django's syntax. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-06-07 07:59:14 +00:00
|
|
|
|
|
|
|
{% verbatim %}
|
|
|
|
{{if dying}}Still alive.{{/if}}
|
|
|
|
{% endverbatim %}
|
|
|
|
|
2012-06-18 22:49:30 +00:00
|
|
|
You can also designate a specific closing tag, allowing the use of
|
2023-02-09 15:48:46 +00:00
|
|
|
``{% endverbatim %}`` as part of the unrendered contents:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-06-07 07:59:14 +00:00
|
|
|
|
2012-06-18 22:49:30 +00:00
|
|
|
{% verbatim myblock %}
|
|
|
|
Avoid template rendering via the {% verbatim %}{% endverbatim %} block.
|
|
|
|
{% endverbatim myblock %}
|
2012-06-07 07:59:14 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: widthratio
|
2007-08-19 10:14:58 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``widthratio``
|
|
|
|
--------------
|
2005-08-29 21:58:21 +00:00
|
|
|
|
2011-10-03 08:06:01 +00: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 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2012-06-06 08:32:03 +00:00
|
|
|
<img src="bar.png" alt="Bar"
|
2018-01-21 07:09:10 +00:00
|
|
|
height="10" width="{% widthratio this_value max_value max_width %}">
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2012-12-18 11:52:30 +00:00
|
|
|
If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100, the
|
|
|
|
image in the above example will be 88 pixels wide
|
|
|
|
(because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2013-08-14 14:14:32 +00:00
|
|
|
In some cases you might want to capture the result of ``widthratio`` in a
|
2023-02-09 15:48:46 +00:00
|
|
|
variable. It can be useful, for instance, in a :ttag:`blocktranslate` like this:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-08-14 14:14:32 +00:00
|
|
|
|
|
|
|
{% widthratio this_value max_value max_width as width %}
|
2019-06-21 16:41:01 +00:00
|
|
|
{% blocktranslate %}The width is: {{ width }}{% endblocktranslate %}
|
2013-08-14 14:14:32 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatetag:: with
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``with``
|
|
|
|
--------
|
2007-03-27 17:25:56 +00:00
|
|
|
|
2007-03-27 17:30:57 +00:00
|
|
|
Caches a complex variable under a simpler name. This is useful when accessing
|
2007-03-27 19:02:32 +00:00
|
|
|
an "expensive" method (e.g., one that hits the database) multiple times.
|
2007-03-27 17:25:56 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2007-03-27 17:25:56 +00:00
|
|
|
|
2010-12-18 02:50:26 +00:00
|
|
|
{% with total=business.employees.count %}
|
2007-03-27 17:30:57 +00:00
|
|
|
{{ total }} employee{{ total|pluralize }}
|
2007-03-27 17:25:56 +00:00
|
|
|
{% endwith %}
|
|
|
|
|
|
|
|
The populated variable (in the example above, ``total``) is only available
|
2007-03-27 19:02:32 +00:00
|
|
|
between the ``{% with %}`` and ``{% endwith %}`` tags.
|
2007-03-27 17:25:56 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can assign more than one context variable:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-12-18 02:50:26 +00:00
|
|
|
|
|
|
|
{% with alpha=1 beta=2 %}
|
|
|
|
...
|
|
|
|
{% endwith %}
|
|
|
|
|
|
|
|
.. note:: The previous more verbose format is still supported:
|
|
|
|
``{% with business.employees.count as total %}``
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. _ref-templates-builtins-filters:
|
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Built-in filter reference
|
2016-01-03 10:56:22 +00:00
|
|
|
=========================
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: add
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``add``
|
|
|
|
-------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Adds the argument to the value.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
{{ value|add:"2" }}
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``4``, then the output will be ``6``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2010-02-22 23:34:33 +00:00
|
|
|
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.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example, if we have:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-02-22 23:34:33 +00:00
|
|
|
|
|
|
|
{{ 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-26 17:05:52 +00:00
|
|
|
|
2010-11-22 06:44:19 +00:00
|
|
|
Strings that can be coerced to integers will be **summed**, not
|
|
|
|
concatenated, as in the first example above.
|
2010-02-22 23:34:33 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: addslashes
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``addslashes``
|
|
|
|
--------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-12-04 21:08:29 +00:00
|
|
|
Adds slashes before quotes. Useful for escaping strings in CSV, for example.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|addslashes }}
|
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If ``value`` is ``"I'm using Django"``, the output will be
|
|
|
|
``"I\'m using Django"``.
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: capfirst
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``capfirst``
|
|
|
|
------------
|
2005-10-20 21:58:01 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Capitalizes the first character of the value. If the first character is not
|
|
|
|
a letter, this filter has no effect.
|
2005-10-20 21:58:01 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|capfirst }}
|
|
|
|
|
|
|
|
If ``value`` is ``"django"``, the output will be ``"Django"``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: center
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``center``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Centers the value in a field of a given width.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
"{{ value|center:"15" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``"Django"``, the output will be ``" Django "``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: cut
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``cut``
|
|
|
|
-------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Removes all values of arg from the given string.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2012-08-04 21:58:31 +00:00
|
|
|
{{ value|cut:" " }}
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If ``value`` is ``"String with spaces"``, the output will be
|
|
|
|
``"Stringwithspaces"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: date
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``date``
|
|
|
|
--------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2009-12-22 17:58:49 +00:00
|
|
|
Formats a date according to the given format.
|
|
|
|
|
2021-04-27 11:09:00 +00:00
|
|
|
Uses a similar format to PHP's `date()
|
|
|
|
<https://www.php.net/manual/en/function.date.php>`_ function with some
|
|
|
|
differences.
|
2010-10-08 15:33:11 +00:00
|
|
|
|
2013-09-21 03:54:48 +00:00
|
|
|
.. note::
|
|
|
|
These format characters are not used in Django outside of templates. They
|
|
|
|
were designed to be compatible with PHP to ease transitioning for designers.
|
|
|
|
|
2013-09-22 18:41:24 +00:00
|
|
|
.. _date-and-time-formatting-specifiers:
|
|
|
|
|
2010-10-08 15:33:11 +00:00
|
|
|
Available format strings:
|
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
================ ======================================== =====================
|
|
|
|
Format character Description Example output
|
|
|
|
================ ======================================== =====================
|
2018-06-04 13:37:42 +00:00
|
|
|
**Day**
|
2019-03-21 14:20:09 +00:00
|
|
|
``d`` Day of the month, 2 digits with ``'01'`` to ``'31'``
|
2011-10-14 00:12:01 +00:00
|
|
|
leading zeros.
|
2019-03-21 14:20:09 +00:00
|
|
|
``j`` Day of the month without leading ``'1'`` to ``'31'``
|
2018-06-04 13:37:42 +00:00
|
|
|
zeros.
|
2019-03-21 14:20:09 +00:00
|
|
|
``D`` Day of the week, textual, 3 letters. ``'Fri'``
|
|
|
|
``l`` Day of the week, textual, long. ``'Friday'``
|
|
|
|
``S`` English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'``
|
2018-06-04 13:37:42 +00:00
|
|
|
month, 2 characters.
|
2019-03-21 14:20:09 +00:00
|
|
|
``w`` Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday)
|
2018-06-04 13:37:42 +00:00
|
|
|
leading zeros.
|
2019-11-15 16:28:48 +00:00
|
|
|
``z`` Day of the year. ``1`` to ``366``
|
2018-06-04 13:37:42 +00:00
|
|
|
**Week**
|
2019-03-21 14:20:09 +00:00
|
|
|
``W`` ISO-8601 week number of year, with ``1``, ``53``
|
2018-06-04 13:37:42 +00:00
|
|
|
weeks starting on Monday.
|
|
|
|
**Month**
|
2019-03-21 14:20:09 +00:00
|
|
|
``m`` Month, 2 digits with leading zeros. ``'01'`` to ``'12'``
|
|
|
|
``n`` Month without leading zeros. ``'1'`` to ``'12'``
|
|
|
|
``M`` Month, textual, 3 letters. ``'Jan'``
|
|
|
|
``b`` Month, textual, 3 letters, lowercase. ``'jan'``
|
|
|
|
``E`` Month, locale specific alternative
|
2011-10-14 00:12:01 +00:00
|
|
|
representation usually used for long
|
|
|
|
date representation. ``'listopada'`` (for Polish locale, as opposed to ``'Listopad'``)
|
2019-03-21 14:20:09 +00:00
|
|
|
``F`` Month, textual, long. ``'January'``
|
|
|
|
``N`` Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'``
|
2018-08-06 18:17:00 +00:00
|
|
|
style. Proprietary extension.
|
2019-03-21 14:20:09 +00:00
|
|
|
``t`` Number of days in the given month. ``28`` to ``31``
|
2018-06-04 13:37:42 +00:00
|
|
|
**Year**
|
2020-10-27 04:12:14 +00:00
|
|
|
``y`` Year, 2 digits with leading zeros. ``'00'`` to ``'99'``
|
2021-05-10 14:33:48 +00:00
|
|
|
``Y`` Year, 4 digits with leading zeros. ``'0001'``, ..., ``'1999'``, ..., ``'9999'``
|
2019-03-21 14:20:09 +00:00
|
|
|
``L`` Boolean for whether it's a leap year. ``True`` or ``False``
|
|
|
|
``o`` ISO-8601 week-numbering year, ``'1999'``
|
2018-06-04 13:37:42 +00:00
|
|
|
corresponding to the ISO-8601 week
|
|
|
|
number (W) which uses leap weeks. See Y
|
|
|
|
for the more common year format.
|
|
|
|
**Time**
|
2019-03-21 14:20:09 +00:00
|
|
|
``g`` Hour, 12-hour format without leading ``'1'`` to ``'12'``
|
2011-10-14 00:12:01 +00:00
|
|
|
zeros.
|
2019-03-21 14:20:09 +00:00
|
|
|
``G`` Hour, 24-hour format without leading ``'0'`` to ``'23'``
|
2011-10-14 00:12:01 +00:00
|
|
|
zeros.
|
2019-03-21 14:20:09 +00:00
|
|
|
``h`` Hour, 12-hour format. ``'01'`` to ``'12'``
|
|
|
|
``H`` Hour, 24-hour format. ``'00'`` to ``'23'``
|
|
|
|
``i`` Minutes. ``'00'`` to ``'59'``
|
|
|
|
``s`` Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'``
|
|
|
|
``u`` Microseconds. ``000000`` to ``999999``
|
|
|
|
``a`` ``'a.m.'`` or ``'p.m.'`` (Note that ``'a.m.'``
|
2018-06-04 13:37:42 +00:00
|
|
|
this is slightly different than PHP's
|
|
|
|
output, because this includes periods
|
|
|
|
to match Associated Press style.)
|
2019-03-21 14:20:09 +00:00
|
|
|
``A`` ``'AM'`` or ``'PM'``. ``'AM'``
|
|
|
|
``f`` Time, in 12-hour hours and minutes, ``'1'``, ``'1:30'``
|
2018-06-04 13:37:42 +00:00
|
|
|
with minutes left off if they're zero.
|
|
|
|
Proprietary extension.
|
2019-03-21 14:20:09 +00:00
|
|
|
``P`` Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'``
|
2011-10-14 00:12:01 +00: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.
|
2018-06-04 13:37:42 +00:00
|
|
|
**Timezone**
|
2019-03-21 14:20:09 +00:00
|
|
|
``e`` Timezone name. Could be in any format,
|
2018-06-04 13:37:42 +00:00
|
|
|
or might return an empty string, ``''``, ``'GMT'``, ``'-500'``, ``'US/Eastern'``, etc.
|
|
|
|
depending on the datetime.
|
2021-07-30 12:41:35 +00:00
|
|
|
``I`` Daylight saving time, whether it's in ``'1'`` or ``'0'``
|
|
|
|
effect or not.
|
2019-03-21 14:20:09 +00:00
|
|
|
``O`` Difference to Greenwich time in hours. ``'+0200'``
|
|
|
|
``T`` Time zone of this machine. ``'EST'``, ``'MDT'``
|
|
|
|
``Z`` Time zone offset in seconds. The ``-43200`` to ``43200``
|
2011-10-14 00:12:01 +00:00
|
|
|
offset for timezones west of UTC is
|
|
|
|
always negative, and for those east of
|
|
|
|
UTC is always positive.
|
2018-06-04 13:37:42 +00:00
|
|
|
**Date/Time**
|
2021-01-04 06:34:53 +00:00
|
|
|
``c`` ISO 8601 format. (Note: unlike other ``2008-01-02T10:30:00.000123+02:00``,
|
2018-06-04 13:37:42 +00:00
|
|
|
formatters, such as "Z", "O" or "r", or ``2008-01-02T10:30:00.000123`` if the datetime is naive
|
|
|
|
the "c" formatter will not add timezone
|
|
|
|
offset if value is a naive datetime
|
|
|
|
(see :class:`datetime.tzinfo`).
|
2019-11-23 12:42:57 +00:00
|
|
|
``r`` :rfc:`RFC 5322 <5322#section-3.3>` ``'Thu, 21 Dec 2000 16:01:07 +0200'``
|
|
|
|
formatted date.
|
2019-03-21 14:20:09 +00:00
|
|
|
``U`` Seconds since the Unix Epoch
|
2018-06-04 13:37:42 +00:00
|
|
|
(January 1 1970 00:00:00 UTC).
|
2011-10-14 00:12:01 +00:00
|
|
|
================ ======================================== =====================
|
2010-10-08 15:33:11 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|date:"D d M Y" }}
|
|
|
|
|
2013-09-22 18:41:24 +00:00
|
|
|
If ``value`` is a :py:class:`~datetime.datetime` object (e.g., the result of
|
2008-03-18 04:21:08 +00:00
|
|
|
``datetime.datetime.now()``), the output will be the string
|
|
|
|
``'Wed 09 Jan 2008'``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2011-05-29 17:41:04 +00:00
|
|
|
The format passed can be one of the predefined ones :setting:`DATE_FORMAT`,
|
|
|
|
:setting:`DATETIME_FORMAT`, :setting:`SHORT_DATE_FORMAT` or
|
|
|
|
:setting:`SHORT_DATETIME_FORMAT`, or a custom format that uses the format
|
|
|
|
specifiers shown in the table above. Note that predefined formats may vary
|
|
|
|
depending on the current locale.
|
2010-02-26 17:05:52 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Assuming that :setting:`LANGUAGE_CODE` is, for example, ``"es"``, then for:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-02-26 17:05:52 +00:00
|
|
|
|
|
|
|
{{ value|date:"SHORT_DATE_FORMAT" }}
|
|
|
|
|
2010-10-25 12:48:56 +00:00
|
|
|
the output would be the string ``"09/01/2008"`` (the ``"SHORT_DATE_FORMAT"``
|
2010-02-26 17:05:52 +00:00
|
|
|
format specifier for the ``es`` locale as shipped with Django is ``"d/m/Y"``).
|
|
|
|
|
2017-03-27 16:53:03 +00:00
|
|
|
When used without a format string, the ``DATE_FORMAT`` format specifier is
|
2023-02-09 15:48:46 +00:00
|
|
|
used. Assuming the same settings as the previous example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-03-31 16:34:24 +00:00
|
|
|
|
|
|
|
{{ value|date }}
|
|
|
|
|
2017-03-27 16:53:03 +00:00
|
|
|
outputs ``9 de Enero de 2008`` (the ``DATE_FORMAT`` format specifier for the
|
2019-11-23 02:38:27 +00:00
|
|
|
``es`` locale is ``r'j \d\e F \d\e Y'``). Both "d" and "e" are
|
|
|
|
backslash-escaped, because otherwise each is a format string that displays the
|
|
|
|
day and the timezone name, respectively.
|
2017-03-27 16:53:03 +00:00
|
|
|
|
2013-09-22 18:41:24 +00:00
|
|
|
You can combine ``date`` with the :tfilter:`time` filter to render a full
|
2023-02-09 15:48:46 +00:00
|
|
|
representation of a ``datetime`` value. E.g.:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-09-22 18:41:24 +00:00
|
|
|
|
|
|
|
{{ value|date:"D d M Y" }} {{ value|time:"H:i" }}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: default
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``default``
|
|
|
|
-----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If value evaluates to ``False``, uses the given default. Otherwise, uses the
|
|
|
|
value.
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|default:"nothing" }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``""`` (the empty string), the output will be ``nothing``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: default_if_none
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``default_if_none``
|
|
|
|
-------------------
|
2005-07-13 01:25:57 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If (and only if) value is ``None``, uses the given default. Otherwise, uses the
|
2008-03-18 04:21:08 +00:00
|
|
|
value.
|
|
|
|
|
|
|
|
Note that if an empty string is given, the default value will *not* be used.
|
2011-10-03 08:06:01 +00:00
|
|
|
Use the :tfilter:`default` filter if you want to fallback for empty strings.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|default_if_none:"nothing" }}
|
|
|
|
|
2017-02-15 13:39:39 +00:00
|
|
|
If ``value`` is ``None``, the output will be ``nothing``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: dictsort
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``dictsort``
|
|
|
|
------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Takes a list of dictionaries and returns that list sorted by the key given in
|
2007-07-15 21:16:32 +00:00
|
|
|
the argument.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|dictsort:"name" }}
|
|
|
|
|
2008-09-09 01:54:20 +00:00
|
|
|
If ``value`` is:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2008-03-18 04:21:08 +00:00
|
|
|
|
2008-03-18 00:14:41 +00:00
|
|
|
[
|
2023-02-28 19:53:28 +00:00
|
|
|
{"name": "zed", "age": 19},
|
|
|
|
{"name": "amy", "age": 22},
|
|
|
|
{"name": "joe", "age": 31},
|
2008-03-18 00:14:41 +00:00
|
|
|
]
|
|
|
|
|
2008-09-09 01:54:20 +00:00
|
|
|
then the output would be:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
[
|
2023-02-28 19:53:28 +00:00
|
|
|
{"name": "amy", "age": 22},
|
|
|
|
{"name": "joe", "age": 31},
|
|
|
|
{"name": "zed", "age": 19},
|
2008-03-18 00:14:41 +00:00
|
|
|
]
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
You can also do more complicated things like:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-11-06 00:38:46 +00:00
|
|
|
|
|
|
|
{% for book in books|dictsort:"author.age" %}
|
2013-11-05 23:17:28 +00:00
|
|
|
* {{ book.title }} ({{ book.author.name }})
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
If ``books`` is:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
[
|
2023-02-28 19:53:28 +00:00
|
|
|
{"title": "1984", "author": {"name": "George", "age": 45}},
|
|
|
|
{"title": "Timequake", "author": {"name": "Kurt", "age": 75}},
|
|
|
|
{"title": "Alice", "author": {"name": "Lewis", "age": 33}},
|
2013-11-05 23:17:28 +00:00
|
|
|
]
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
then the output would be:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-11-05 23:17:28 +00:00
|
|
|
|
|
|
|
* Alice (Lewis)
|
|
|
|
* 1984 (George)
|
|
|
|
* Timequake (Kurt)
|
|
|
|
|
2015-11-05 10:59:56 +00:00
|
|
|
``dictsort`` can also order a list of lists (or any other object implementing
|
2023-02-09 15:48:46 +00:00
|
|
|
``__getitem__()``) by elements at specified index. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2015-11-05 10:59:56 +00:00
|
|
|
|
|
|
|
{{ value|dictsort:0 }}
|
|
|
|
|
|
|
|
If ``value`` is:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
[
|
2023-02-28 19:53:28 +00:00
|
|
|
("a", "42"),
|
|
|
|
("c", "string"),
|
|
|
|
("b", "foo"),
|
2015-11-05 10:59:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
then the output would be:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
[
|
2023-02-28 19:53:28 +00:00
|
|
|
("a", "42"),
|
|
|
|
("b", "foo"),
|
|
|
|
("c", "string"),
|
2015-11-05 10:59:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
You must pass the index as an integer rather than a string. The following
|
2023-02-09 15:48:46 +00:00
|
|
|
produce empty output:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2015-11-05 10:59:56 +00:00
|
|
|
|
|
|
|
{{ values|dictsort:"0" }}
|
|
|
|
|
2021-12-27 13:53:18 +00:00
|
|
|
Ordering by elements at specified index is not supported on dictionaries.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.2.26
|
|
|
|
|
|
|
|
In older versions, ordering elements at specified index was supported on
|
|
|
|
dictionaries.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: dictsortreversed
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``dictsortreversed``
|
|
|
|
--------------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00: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 15:15:26 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: divisibleby
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``divisibleby``
|
|
|
|
---------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Returns ``True`` if the value is divisible by the argument.
|
2005-10-14 03:48:27 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
{{ value|divisibleby:"3" }}
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
If ``value`` is ``21``, the output would be ``True``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: escape
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``escape``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Escapes a string's HTML. Specifically, it makes these replacements:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``<`` is converted to ``<``
|
|
|
|
* ``>`` is converted to ``>``
|
2019-04-24 11:30:34 +00:00
|
|
|
* ``'`` (single quote) is converted to ``'``
|
2011-10-14 00:12:01 +00:00
|
|
|
* ``"`` (double quote) is converted to ``"``
|
|
|
|
* ``&`` is converted to ``&``
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-11-14 12:58:53 +00:00
|
|
|
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
|
2011-10-03 08:06:01 +00:00
|
|
|
multiple escaping passes to be applied, use the :tfilter:`force_escape` filter.
|
2007-11-14 12:58:53 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example, you can apply ``escape`` to fields when :ttag:`autoescape` is off:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
{{ title|escape }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2023-05-24 16:59:00 +00:00
|
|
|
.. admonition:: Chaining ``escape`` with other filters
|
|
|
|
|
|
|
|
As mentioned in the :ttag:`autoescape` section, when filters including
|
|
|
|
``escape`` are chained together, it can result in unexpected outcomes if
|
|
|
|
preceding filters mark a potentially unsafe string as safe due to the lack
|
|
|
|
of escaping caused by :ttag:`autoescape` being ``off``.
|
|
|
|
|
|
|
|
In such cases, chaining ``escape`` would not reescape strings that have
|
|
|
|
already been marked as safe.
|
|
|
|
|
2023-05-24 16:59:56 +00:00
|
|
|
This is especially important when using filters that operate on sequences,
|
|
|
|
for example :tfilter:`join`. If you need to escape each element in a
|
|
|
|
sequence, use the dedicated :tfilter:`escapeseq` filter.
|
2023-05-19 22:33:51 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: escapejs
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``escapejs``
|
|
|
|
------------
|
2007-12-04 21:08:29 +00:00
|
|
|
|
2023-05-25 15:39:15 +00:00
|
|
|
Escapes characters for use as a whole JavaScript string literal, within single
|
|
|
|
or double quotes, as below. This filter does not make the string safe for use
|
|
|
|
in *"JavaScript template literals"* (the JavaScript backtick syntax). Any other
|
|
|
|
uses not listed above are not supported. It is generally recommended that data
|
|
|
|
should be passed using HTML ``data-`` attributes, or the :tfilter:`json_script`
|
|
|
|
filter, rather than in embedded JavaScript.
|
2007-12-04 21:08:29 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2023-05-25 15:39:15 +00:00
|
|
|
<script>
|
|
|
|
let myValue = '{{ value|escapejs }}'
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2023-05-19 22:33:51 +00:00
|
|
|
.. templatefilter:: escapeseq
|
|
|
|
|
|
|
|
``escapeseq``
|
|
|
|
-------------
|
|
|
|
|
|
|
|
.. versionadded:: 5.0
|
|
|
|
|
|
|
|
Applies the :tfilter:`escape` filter to each element of a sequence. Useful in
|
|
|
|
conjunction with other filters that operate on sequences, such as
|
|
|
|
:tfilter:`join`. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
{{ my_list|escapeseq|join:", " }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: filesizeformat
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``filesizeformat``
|
|
|
|
------------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Formats the value like a 'human-readable' file size (i.e. ``'13 KB'``,
|
2016-01-10 16:48:16 +00:00
|
|
|
``'4.1 MB'``, ``'102 bytes'``, etc.).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|filesizeformat }}
|
|
|
|
|
|
|
|
If ``value`` is 123456789, the output would be ``117.7 MB``.
|
|
|
|
|
2012-11-23 12:00:08 +00:00
|
|
|
.. admonition:: File sizes and SI units
|
|
|
|
|
|
|
|
Strictly speaking, ``filesizeformat`` does not conform to the International
|
|
|
|
System of Units which recommends using KiB, MiB, GiB, etc. when byte sizes
|
|
|
|
are calculated in powers of 1024 (which is the case here). Instead, Django
|
|
|
|
uses traditional unit names (KB, MB, GB, etc.) corresponding to names that
|
|
|
|
are more commonly used.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: first
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``first``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Returns the first item in a list.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|first }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c']``, the output will be ``'a'``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: floatformat
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``floatformat``
|
|
|
|
---------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-01-03 05:29:34 +00: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 04:44:21 +00:00
|
|
|
|
2007-12-11 06:55:25 +00:00
|
|
|
============ =========================== ========
|
|
|
|
``value`` Template Output
|
|
|
|
============ =========================== ========
|
|
|
|
``34.23234`` ``{{ value|floatformat }}`` ``34.2``
|
|
|
|
``34.00000`` ``{{ value|floatformat }}`` ``34``
|
|
|
|
``34.26000`` ``{{ value|floatformat }}`` ``34.3``
|
|
|
|
============ =========================== ========
|
2007-11-04 02:48:44 +00:00
|
|
|
|
|
|
|
If used with a numeric integer argument, ``floatformat`` rounds a number to
|
2007-12-01 17:29:45 +00:00
|
|
|
that many decimal places. For example:
|
2007-11-04 02:48:44 +00:00
|
|
|
|
2007-12-11 06:55:25 +00: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 02:48:44 +00:00
|
|
|
|
2012-11-05 00:35:40 +00:00
|
|
|
Particularly useful is passing 0 (zero) as the argument which will round the
|
|
|
|
float to the nearest integer.
|
|
|
|
|
|
|
|
============ ================================ ==========
|
|
|
|
``value`` Template Output
|
|
|
|
============ ================================ ==========
|
|
|
|
``34.23234`` ``{{ value|floatformat:"0" }}`` ``34``
|
|
|
|
``34.00000`` ``{{ value|floatformat:"0" }}`` ``34``
|
|
|
|
``39.56000`` ``{{ value|floatformat:"0" }}`` ``40``
|
|
|
|
============ ================================ ==========
|
|
|
|
|
2007-11-04 02:48:44 +00: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 06:55:25 +00: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 02:48:44 +00:00
|
|
|
|
2020-10-12 03:34:32 +00:00
|
|
|
If the argument passed to ``floatformat`` has the ``g`` suffix, it will force
|
|
|
|
grouping by the :setting:`THOUSAND_SEPARATOR` for the active locale. For
|
|
|
|
example, when the active locale is ``en`` (English):
|
|
|
|
|
|
|
|
============ ================================= =============
|
|
|
|
``value`` Template Output
|
|
|
|
============ ================================= =============
|
|
|
|
``34232.34`` ``{{ value|floatformat:"2g" }}`` ``34,232.34``
|
|
|
|
``34232.06`` ``{{ value|floatformat:"g" }}`` ``34,232.1``
|
|
|
|
``34232.00`` ``{{ value|floatformat:"-3g" }}`` ``34,232``
|
|
|
|
============ ================================= =============
|
|
|
|
|
2021-09-08 06:37:27 +00:00
|
|
|
Output is always localized (independently of the :ttag:`{% localize off %}
|
|
|
|
<localize>` tag) unless the argument passed to ``floatformat`` has the ``u``
|
|
|
|
suffix, which will force disabling localization. For example, when the active
|
|
|
|
locale is ``pl`` (Polish):
|
|
|
|
|
|
|
|
============ ================================= =============
|
|
|
|
``value`` Template Output
|
|
|
|
============ ================================= =============
|
|
|
|
``34.23234`` ``{{ value|floatformat:"3" }}`` ``34,232``
|
|
|
|
``34.23234`` ``{{ value|floatformat:"3u" }}`` ``34.232``
|
|
|
|
============ ================================= =============
|
|
|
|
|
2007-11-04 02:48:44 +00:00
|
|
|
Using ``floatformat`` with no argument is equivalent to using ``floatformat``
|
|
|
|
with an argument of ``-1``.
|
2007-01-03 05:29:34 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: force_escape
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``force_escape``
|
|
|
|
----------------
|
2007-11-14 12:58:53 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Applies HTML escaping to a string (see the :tfilter:`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 :tfilter:`escape` filter.
|
2007-11-14 12:58:53 +00:00
|
|
|
|
2012-11-13 00:12:27 +00:00
|
|
|
For example, if you want to catch the ``<p>`` HTML elements created by
|
2023-02-09 15:48:46 +00:00
|
|
|
the :tfilter:`linebreaks` filter:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
|
|
|
{% autoescape off %}
|
|
|
|
{{ body|linebreaks|force_escape }}
|
|
|
|
{% endautoescape %}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: get_digit
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``get_digit``
|
|
|
|
-------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00: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 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
{{ value|get_digit:"2" }}
|
|
|
|
|
|
|
|
If ``value`` is ``123456789``, the output will be ``8``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2010-05-07 00:57:17 +00:00
|
|
|
.. templatefilter:: iriencode
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``iriencode``
|
|
|
|
-------------
|
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 12:11:04 +00:00
|
|
|
|
|
|
|
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
|
2011-10-03 08:06:01 +00:00
|
|
|
:tfilter:`urlencode` filter.
|
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 12:11:04 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|iriencode }}
|
|
|
|
|
2024-03-09 06:58:20 +00:00
|
|
|
If ``value`` is ``"?test=I ♥ Django"``, the output will be
|
|
|
|
``"?test=I%20%E2%99%A5%20Django"``.
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: join
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``join``
|
|
|
|
--------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 00:14:41 +00:00
|
|
|
Joins a list with a string, like Python's ``str.join(list)``
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|join:" // " }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c']``, the output will be the string
|
|
|
|
``"a // b // c"``.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2017-10-12 18:59:38 +00:00
|
|
|
.. templatefilter:: json_script
|
|
|
|
|
|
|
|
``json_script``
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Safely outputs a Python object as JSON, wrapped in a ``<script>`` tag, ready
|
|
|
|
for use with JavaScript.
|
|
|
|
|
2021-11-19 15:52:57 +00:00
|
|
|
**Argument:** The optional HTML "id" of the ``<script>`` tag.
|
2017-10-12 18:59:38 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2017-10-12 18:59:38 +00:00
|
|
|
|
|
|
|
{{ value|json_script:"hello-data" }}
|
|
|
|
|
2018-07-04 17:15:35 +00:00
|
|
|
If ``value`` is the dictionary ``{'hello': 'world'}``, the output will be:
|
2017-10-12 18:59:38 +00:00
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<script id="hello-data" type="application/json">{"hello": "world"}</script>
|
|
|
|
|
|
|
|
The resulting data can be accessed in JavaScript like this:
|
|
|
|
|
|
|
|
.. code-block:: javascript
|
|
|
|
|
2020-06-24 09:41:10 +00:00
|
|
|
const value = JSON.parse(document.getElementById('hello-data').textContent);
|
2017-10-12 18:59:38 +00:00
|
|
|
|
|
|
|
XSS attacks are mitigated by escaping the characters "<", ">" and "&". For
|
|
|
|
example if ``value`` is ``{'hello': 'world</script>&'}``, the output is:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<script id="hello-data" type="application/json">{"hello": "world\\u003C/script\\u003E\\u0026amp;"}</script>
|
|
|
|
|
|
|
|
This is compatible with a strict Content Security Policy that prohibits in-page
|
|
|
|
script execution. It also maintains a clean separation between passive data and
|
|
|
|
executable code.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: last
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``last``
|
|
|
|
--------
|
2008-01-06 03:53:33 +00:00
|
|
|
|
|
|
|
Returns the last item in a list.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|last }}
|
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output will be the
|
|
|
|
string ``"d"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: length
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``length``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Returns the length of the value. This works for both strings and lists.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|length }}
|
|
|
|
|
2014-03-22 15:33:37 +00:00
|
|
|
If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
|
|
|
|
``4``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2015-09-12 20:27:30 +00:00
|
|
|
The filter returns ``0`` for an undefined variable.
|
2013-10-15 05:15:13 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: length_is
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``length_is``
|
|
|
|
-------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2021-04-22 15:55:59 +00:00
|
|
|
.. deprecated:: 4.2
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Returns ``True`` if the value's length is the argument, or ``False`` otherwise.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
{{ value|length_is:"4" }}
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2014-03-22 15:33:37 +00:00
|
|
|
If ``value`` is ``['a', 'b', 'c', 'd']`` or ``"abcd"``, the output will be
|
|
|
|
``True``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: linebreaks
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``linebreaks``
|
|
|
|
--------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-09-14 21:46:38 +00:00
|
|
|
Replaces line breaks in plain text with appropriate HTML; a single
|
2018-01-21 07:09:10 +00:00
|
|
|
newline becomes an HTML line break (``<br>``) and a new line
|
2007-09-14 21:46:38 +00:00
|
|
|
followed by a blank line becomes a paragraph break (``</p>``).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|linebreaks }}
|
|
|
|
|
2018-01-21 07:09:10 +00:00
|
|
|
If ``value`` is ``Joel\nis a slug``, the output will be ``<p>Joel<br>is a
|
2008-03-18 00:14:41 +00:00
|
|
|
slug</p>``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: linebreaksbr
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``linebreaksbr``
|
|
|
|
----------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2007-09-14 21:46:38 +00:00
|
|
|
Converts all newlines in a piece of plain text to HTML line breaks
|
2018-01-21 07:09:10 +00:00
|
|
|
(``<br>``).
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|linebreaksbr }}
|
|
|
|
|
2018-01-21 07:09:10 +00:00
|
|
|
If ``value`` is ``Joel\nis a slug``, the output will be ``Joel<br>is a
|
2010-01-12 23:36:19 +00:00
|
|
|
slug``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: linenumbers
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``linenumbers``
|
|
|
|
---------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Displays text with line numbers.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|linenumbers }}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
If ``value`` is:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
one
|
|
|
|
two
|
|
|
|
three
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
the output will be:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
1. one
|
|
|
|
2. two
|
|
|
|
3. three
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: ljust
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``ljust``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Left-aligns the value in a field of a given width.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
**Argument:** field size
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
"{{ value|ljust:"10" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``Django``, the output will be ``"Django "``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: lower
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``lower``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Converts a string into all lowercase.
|
2005-09-24 17:39:20 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|lower }}
|
|
|
|
|
2015-11-13 11:25:34 +00:00
|
|
|
If ``value`` is ``Totally LOVING this Album!``, the output will be
|
|
|
|
``totally loving this album!``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: make_list
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``make_list``
|
|
|
|
-------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-06-26 21:27:44 +00:00
|
|
|
Returns the value turned into a list. For a string, it's a list of characters.
|
2017-01-20 21:04:05 +00:00
|
|
|
For an integer, the argument is cast to a string before creating a list.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|make_list }}
|
|
|
|
|
2008-07-26 03:08:53 +00:00
|
|
|
If ``value`` is the string ``"Joel"``, the output would be the list
|
2014-03-22 20:30:49 +00:00
|
|
|
``['J', 'o', 'e', 'l']``. If ``value`` is ``123``, the output will be the
|
|
|
|
list ``['1', '2', '3']``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: phone2numeric
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``phone2numeric``
|
|
|
|
-----------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2006-02-13 15:33:25 +00:00
|
|
|
Converts a phone number (possibly containing letters) to its numerical
|
2010-01-12 23:36:19 +00:00
|
|
|
equivalent.
|
2006-02-13 15:33:25 +00:00
|
|
|
|
|
|
|
The input doesn't have to be a valid phone number. This will happily convert
|
|
|
|
any string.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|phone2numeric }}
|
|
|
|
|
|
|
|
If ``value`` is ``800-COLLECT``, the output will be ``800-2655328``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: pluralize
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``pluralize``
|
|
|
|
-------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2019-04-27 16:08:39 +00:00
|
|
|
Returns a plural suffix if the value is not ``1``, ``'1'``, or an object of
|
|
|
|
length 1. By default, this suffix is ``'s'``.
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
You have {{ num_messages }} message{{ num_messages|pluralize }}.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2010-01-12 23:36:19 +00: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-21 18:27:33 +00:00
|
|
|
For words that require a suffix other than ``'s'``, you can provide an alternate
|
2006-07-04 06:18:39 +00:00
|
|
|
suffix as a parameter to the filter.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-07-04 06:18:39 +00:00
|
|
|
|
2009-05-27 16:26:56 +00:00
|
|
|
You have {{ num_walruses }} walrus{{ num_walruses|pluralize:"es" }}.
|
2006-07-04 06:18:39 +00:00
|
|
|
|
|
|
|
For words that don't pluralize by simple suffix, you can specify both a
|
|
|
|
singular and plural suffix, separated by a comma.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2006-07-04 06:18:39 +00:00
|
|
|
|
|
|
|
You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
|
2009-02-22 06:09:28 +00:00
|
|
|
|
2019-06-21 16:41:01 +00:00
|
|
|
.. note:: Use :ttag:`blocktranslate` to pluralize translated strings.
|
2011-06-16 16:34:20 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: pprint
|
2006-07-04 06:18:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``pprint``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2011-09-04 21:17:30 +00:00
|
|
|
A wrapper around :func:`pprint.pprint` -- for debugging, really.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: random
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``random``
|
|
|
|
----------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Returns a random item from the given list.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|random }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is the list ``['a', 'b', 'c', 'd']``, the output could be ``"b"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: rjust
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``rjust``
|
|
|
|
---------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Right-aligns the value in a field of a given width.
|
|
|
|
|
|
|
|
**Argument:** field size
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
"{{ value|rjust:"10" }}"
|
|
|
|
|
|
|
|
If ``value`` is ``Django``, the output will be ``" Django"``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: safe
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``safe``
|
|
|
|
--------
|
2007-11-14 12:58:53 +00:00
|
|
|
|
|
|
|
Marks a string as not requiring further HTML escaping prior to output. When
|
|
|
|
autoescaping is off, this filter has no effect.
|
|
|
|
|
2010-05-09 06:45:21 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you are chaining filters, a filter applied after ``safe`` can
|
|
|
|
make the contents unsafe again. For example, the following code
|
2023-02-09 15:48:46 +00:00
|
|
|
prints the variable as is, unescaped:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-05-09 06:45:21 +00:00
|
|
|
|
|
|
|
{{ var|safe|escape }}
|
|
|
|
|
2009-03-02 08:16:33 +00:00
|
|
|
.. templatefilter:: safeseq
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``safeseq``
|
|
|
|
-----------
|
2009-03-02 08:16:33 +00:00
|
|
|
|
2013-08-21 17:53:38 +00:00
|
|
|
Applies the :tfilter:`safe` filter to each element of a sequence. Useful in
|
2009-03-02 08:16:33 +00:00
|
|
|
conjunction with other filters that operate on sequences, such as
|
2023-02-09 15:48:46 +00:00
|
|
|
:tfilter:`join`. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-03-02 08:16:33 +00:00
|
|
|
|
|
|
|
{{ 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-23 22:25:40 +00:00
|
|
|
.. templatefilter:: slice
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``slice``
|
|
|
|
---------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Returns a slice of the list.
|
|
|
|
|
2024-01-18 22:03:20 +00:00
|
|
|
Uses the same syntax as Python's list slicing. See the `Python documentation
|
|
|
|
<https://docs.python.org/3/tutorial/introduction.html#lists>`_ for an
|
2021-04-27 11:09:00 +00:00
|
|
|
introduction.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-09-09 01:54:20 +00:00
|
|
|
|
|
|
|
{{ some_list|slice:":2" }}
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2010-01-12 23:36:19 +00:00
|
|
|
If ``some_list`` is ``['a', 'b', 'c']``, the output will be ``['a', 'b']``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: slugify
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``slugify``
|
|
|
|
-----------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2014-10-13 22:15:36 +00:00
|
|
|
Converts to ASCII. Converts spaces to hyphens. Removes characters that aren't
|
|
|
|
alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips
|
|
|
|
leading and trailing whitespace.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|slugify }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"joel-is-a-slug"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: stringformat
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``stringformat``
|
|
|
|
----------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Formats the variable according to the argument, a string formatting specifier.
|
2016-05-08 22:07:43 +00:00
|
|
|
This specifier uses the :ref:`old-string-formatting` syntax, with the exception
|
|
|
|
that the leading "%" is dropped.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2012-11-18 00:29:31 +00:00
|
|
|
{{ value|stringformat:"E" }}
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2012-11-18 00:29:31 +00:00
|
|
|
If ``value`` is ``10``, the output will be ``1.000000E+01``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: striptags
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``striptags``
|
|
|
|
-------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2014-03-20 15:50:50 +00:00
|
|
|
Makes all possible efforts to strip all [X]HTML tags.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|striptags }}
|
|
|
|
|
2008-03-18 04:21:08 +00: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 00:14:41 +00:00
|
|
|
|
2014-03-20 15:50:50 +00:00
|
|
|
.. admonition:: No safety guarantee
|
|
|
|
|
|
|
|
Note that ``striptags`` doesn't give any guarantee about its output being
|
2014-08-07 13:20:59 +00:00
|
|
|
HTML safe, particularly with non valid HTML input. So **NEVER** apply the
|
|
|
|
``safe`` filter to a ``striptags`` output. If you are looking for something
|
2023-05-31 07:32:45 +00:00
|
|
|
more robust, consider using a third-party HTML sanitizing tool.
|
2014-03-20 15:50:50 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: time
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``time``
|
|
|
|
--------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2009-12-22 17:58:49 +00:00
|
|
|
Formats a time according to the given format.
|
|
|
|
|
2011-05-29 17:41:04 +00:00
|
|
|
Given format can be the predefined one :setting:`TIME_FORMAT`, or a custom
|
|
|
|
format, same as the :tfilter:`date` filter. Note that the predefined format
|
2013-11-02 01:29:39 +00:00
|
|
|
is locale-dependent.
|
2009-12-22 17:58:49 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|time:"H:i" }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is equivalent to ``datetime.datetime.now()``, the output will be
|
|
|
|
the string ``"01:23"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2019-11-23 02:38:27 +00:00
|
|
|
Note that you can backslash-escape a format string if you want to use the
|
|
|
|
"raw" value. In this example, both "h" and "m" are backslash-escaped, because
|
|
|
|
otherwise each is a format string that displays the hour and the month,
|
2023-02-09 15:48:46 +00:00
|
|
|
respectively:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2019-11-23 02:38:27 +00:00
|
|
|
|
2022-02-21 05:13:36 +00:00
|
|
|
{{ value|time:"H\h i\m" }}
|
2019-11-23 02:38:27 +00:00
|
|
|
|
|
|
|
This would display as "01h 23m".
|
|
|
|
|
2010-02-26 17:05:52 +00:00
|
|
|
Another example:
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Assuming that :setting:`LANGUAGE_CODE` is, for example, ``"de"``, then for:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-02-26 17:05:52 +00:00
|
|
|
|
|
|
|
{{ value|time:"TIME_FORMAT" }}
|
|
|
|
|
2017-04-11 16:30:58 +00:00
|
|
|
the output will be the string ``"01:23"`` (The ``"TIME_FORMAT"`` format
|
|
|
|
specifier for the ``de`` locale as shipped with Django is ``"H:i"``).
|
2010-02-26 17:05:52 +00:00
|
|
|
|
2013-09-22 18:41:24 +00:00
|
|
|
The ``time`` filter will only accept parameters in the format string that
|
2019-06-17 14:54:55 +00:00
|
|
|
relate to the time of day, not the date. If you need to format a ``date``
|
|
|
|
value, use the :tfilter:`date` filter instead (or along with :tfilter:`time` if
|
|
|
|
you need to render a full :py:class:`~datetime.datetime` value).
|
2013-09-22 18:41:24 +00:00
|
|
|
|
|
|
|
There is one exception the above rule: When passed a ``datetime`` value with
|
|
|
|
attached timezone information (a :ref:`time-zone-aware
|
|
|
|
<naive_vs_aware_datetimes>` ``datetime`` instance) the ``time`` filter will
|
|
|
|
accept the timezone-related :ref:`format specifiers
|
|
|
|
<date-and-time-formatting-specifiers>` ``'e'``, ``'O'`` , ``'T'`` and ``'Z'``.
|
|
|
|
|
2017-03-27 16:53:03 +00:00
|
|
|
When used without a format string, the ``TIME_FORMAT`` format specifier is
|
2023-02-09 15:48:46 +00:00
|
|
|
used:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2009-03-31 16:34:24 +00:00
|
|
|
|
|
|
|
{{ value|time }}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
is the same as:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2017-03-27 16:53:03 +00:00
|
|
|
|
|
|
|
{{ value|time:"TIME_FORMAT" }}
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: timesince
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``timesince``
|
|
|
|
-------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
Formats a date as the time since that date (e.g., "4 days, 6 hours").
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2006-06-21 06:56:08 +00: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 02:30:42 +00:00
|
|
|
June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
|
2023-02-09 15:48:46 +00:00
|
|
|
then the following would return "8 hours":
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
|
|
|
{{ blog_date|timesince:comment_date }}
|
2006-06-21 06:56:08 +00:00
|
|
|
|
2008-08-26 08:08:55 +00:00
|
|
|
Comparing offset-naive and offset-aware datetimes will return an empty string.
|
|
|
|
|
2007-09-17 04:50:12 +00: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-23 22:25:40 +00:00
|
|
|
.. templatefilter:: timeuntil
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``timeuntil``
|
|
|
|
-------------
|
2006-06-21 06:56:08 +00:00
|
|
|
|
|
|
|
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 04:50:12 +00:00
|
|
|
``{{ conference_date|timeuntil }}`` will return "4 weeks".
|
2006-06-21 06:56:08 +00: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
|
2023-02-09 15:48:46 +00:00
|
|
|
2006, then the following will return "1 week":
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-11-09 00:32:16 +00:00
|
|
|
|
|
|
|
{{ conference_date|timeuntil:from_date }}
|
2007-09-17 04:50:12 +00:00
|
|
|
|
2008-08-26 08:08:55 +00:00
|
|
|
Comparing offset-naive and offset-aware datetimes will return an empty string.
|
|
|
|
|
2007-09-17 04:50:12 +00: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 06:56:08 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: title
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``title``
|
|
|
|
---------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2013-10-30 13:33:28 +00:00
|
|
|
Converts a string into titlecase by making words start with an uppercase
|
|
|
|
character and the remaining characters lowercase. This tag makes no effort to
|
|
|
|
keep "trivial words" in lowercase.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|title }}
|
|
|
|
|
2013-10-30 13:33:28 +00:00
|
|
|
If ``value`` is ``"my FIRST post"``, the output will be ``"My First Post"``.
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2011-07-14 13:47:10 +00:00
|
|
|
.. templatefilter:: truncatechars
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``truncatechars``
|
|
|
|
-----------------
|
2011-07-14 13:47:10 +00:00
|
|
|
|
|
|
|
Truncates a string if it is longer than the specified number of characters.
|
2018-08-21 13:28:51 +00:00
|
|
|
Truncated strings will end with a translatable ellipsis character ("…").
|
2011-07-14 13:47:10 +00:00
|
|
|
|
|
|
|
**Argument:** Number of characters to truncate to
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-07-14 13:47:10 +00:00
|
|
|
|
2018-08-21 13:28:51 +00:00
|
|
|
{{ value|truncatechars:7 }}
|
2011-07-14 13:47:10 +00:00
|
|
|
|
2018-08-21 13:28:51 +00:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel i…"``.
|
2011-07-14 13:47:10 +00:00
|
|
|
|
2013-01-10 09:27:20 +00:00
|
|
|
.. templatefilter:: truncatechars_html
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``truncatechars_html``
|
|
|
|
----------------------
|
2013-01-10 09:27:20 +00:00
|
|
|
|
|
|
|
Similar to :tfilter:`truncatechars`, 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.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2013-01-10 09:27:20 +00:00
|
|
|
|
2018-08-21 13:28:51 +00:00
|
|
|
{{ value|truncatechars_html:7 }}
|
2013-01-10 09:27:20 +00:00
|
|
|
|
|
|
|
If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
|
2018-08-21 13:28:51 +00:00
|
|
|
``"<p>Joel i…</p>"``.
|
2013-01-10 09:27:20 +00:00
|
|
|
|
|
|
|
Newlines in the HTML content will be preserved.
|
|
|
|
|
2023-09-19 12:51:48 +00:00
|
|
|
.. admonition:: Size of input string
|
|
|
|
|
|
|
|
Processing large, potentially malformed HTML strings can be
|
|
|
|
resource-intensive and impact service performance. ``truncatechars_html``
|
|
|
|
limits input to the first five million characters.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2.22
|
|
|
|
|
|
|
|
In older versions, strings over five million characters were processed.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: truncatewords
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``truncatewords``
|
|
|
|
-----------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Truncates a string after a certain number of words.
|
|
|
|
|
|
|
|
**Argument:** Number of words to truncate after
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|truncatewords:2 }}
|
|
|
|
|
2018-08-21 13:28:51 +00:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is …"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2010-08-07 14:57:24 +00:00
|
|
|
Newlines within the string will be removed.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: truncatewords_html
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``truncatewords_html``
|
|
|
|
----------------------
|
2007-02-10 05:22:12 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
Similar to :tfilter:`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.
|
2007-02-10 05:22:12 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
This is less efficient than :tfilter:`truncatewords`, so should only be used
|
|
|
|
when it is being passed HTML text.
|
2007-02-10 05:22:12 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|truncatewords_html:2 }}
|
|
|
|
|
|
|
|
If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
|
2018-08-21 13:28:51 +00:00
|
|
|
``"<p>Joel is …</p>"``.
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2010-08-07 14:57:24 +00:00
|
|
|
Newlines in the HTML content will be preserved.
|
2023-09-19 12:51:48 +00:00
|
|
|
|
|
|
|
.. admonition:: Size of input string
|
|
|
|
|
|
|
|
Processing large, potentially malformed HTML strings can be
|
|
|
|
resource-intensive and impact service performance. ``truncatewords_html``
|
|
|
|
limits input to the first five million characters.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.2.22
|
|
|
|
|
|
|
|
In older versions, strings over five million characters were processed.
|
2010-08-07 14:57:24 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: unordered_list
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``unordered_list``
|
|
|
|
------------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Recursively takes a self-nested list and returns an HTML unordered list --
|
2022-03-10 09:18:31 +00:00
|
|
|
WITHOUT opening and closing ``<ul>`` tags.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2011-10-03 08:06:01 +00:00
|
|
|
The list is assumed to be in the proper format. For example, if ``var``
|
|
|
|
contains ``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then
|
2023-02-09 15:48:46 +00:00
|
|
|
``{{ var|unordered_list }}`` would return:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
<li>States
|
|
|
|
<ul>
|
|
|
|
<li>Kansas
|
|
|
|
<ul>
|
|
|
|
<li>Lawrence</li>
|
|
|
|
<li>Topeka</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
<li>Illinois</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: upper
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``upper``
|
|
|
|
---------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Converts a string into all uppercase.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|upper }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``"JOEL IS A SLUG"``.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: urlencode
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``urlencode``
|
|
|
|
-------------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
|
|
|
Escapes a value for use in a URL.
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|urlencode }}
|
|
|
|
|
2015-11-29 16:29:46 +00:00
|
|
|
If ``value`` is ``"https://www.example.org/foo?a=b&c=d"``, the output will be
|
|
|
|
``"https%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"``.
|
2010-01-12 23:36:19 +00:00
|
|
|
|
2010-09-13 23:01:34 +00:00
|
|
|
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
|
2023-02-09 15:48:46 +00:00
|
|
|
provided when *all* characters should be escaped. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-09-13 23:01:34 +00:00
|
|
|
|
|
|
|
{{ value|urlencode:"" }}
|
|
|
|
|
2015-11-29 16:29:46 +00:00
|
|
|
If ``value`` is ``"https://www.example.org/"``, the output will be
|
|
|
|
``"https%3A%2F%2Fwww.example.org%2F"``.
|
2010-09-13 23:01:34 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: urlize
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``urlize``
|
|
|
|
----------
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2013-09-13 15:45:42 +00:00
|
|
|
Converts URLs and email addresses in text into clickable links.
|
2005-11-06 23:30:06 +00:00
|
|
|
|
2012-01-08 21:36:22 +00:00
|
|
|
This template tag works on links prefixed with ``http://``, ``https://``, or
|
2015-11-29 16:29:46 +00:00
|
|
|
``www.``. For example, ``https://goo.gl/aia1t`` will get converted but
|
2012-01-08 21:36:22 +00:00
|
|
|
``goo.gl/aia1t`` won't.
|
|
|
|
|
|
|
|
It also supports domain-only links ending in one of the original top level
|
|
|
|
domains (``.com``, ``.edu``, ``.gov``, ``.int``, ``.mil``, ``.net``, and
|
2012-02-03 20:45:45 +00:00
|
|
|
``.org``). For example, ``djangoproject.com`` gets converted.
|
2012-01-08 16:08:43 +00:00
|
|
|
|
2011-06-27 03:32:10 +00:00
|
|
|
Links can have trailing punctuation (periods, commas, close-parens) and leading
|
2012-02-03 20:45:45 +00:00
|
|
|
punctuation (opening parens), and ``urlize`` will still do the right thing.
|
2010-12-28 22:53:28 +00:00
|
|
|
|
|
|
|
Links generated by ``urlize`` have a ``rel="nofollow"`` attribute added
|
|
|
|
to them.
|
2007-08-19 08:50:51 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|urlize }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``"Check out www.djangoproject.com"``, the output will be
|
2010-12-28 22:53:28 +00:00
|
|
|
``"Check out <a href="http://www.djangoproject.com"
|
|
|
|
rel="nofollow">www.djangoproject.com</a>"``.
|
|
|
|
|
2013-09-13 15:45:42 +00:00
|
|
|
In addition to web links, ``urlize`` also converts email addresses into
|
|
|
|
``mailto:`` links. If ``value`` is
|
|
|
|
``"Send questions to foo@example.com"``, the output will be
|
2015-03-31 13:36:25 +00:00
|
|
|
``"Send questions to <a href="mailto:foo@example.com">foo@example.com</a>"``.
|
2013-09-13 15:45:42 +00:00
|
|
|
|
2010-12-28 22:53:28 +00:00
|
|
|
The ``urlize`` filter also takes an optional parameter ``autoescape``. If
|
|
|
|
``autoescape`` is ``True``, the link text and URLs will be escaped using
|
|
|
|
Django's built-in :tfilter:`escape` filter. The default value for
|
|
|
|
``autoescape`` is ``True``.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2019-06-29 01:41:36 +00:00
|
|
|
If ``urlize`` is applied to text that already contains HTML markup, or to
|
|
|
|
email addresses that contain single quotes (``'``), things won't work as
|
|
|
|
expected. Apply this filter only to plain text.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: urlizetrunc
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``urlizetrunc``
|
|
|
|
---------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2015-08-08 12:09:27 +00:00
|
|
|
Converts URLs and email addresses into clickable links just like urlize_, but
|
|
|
|
truncates URLs longer than the given character limit.
|
2007-08-19 08:50:51 +00:00
|
|
|
|
2010-12-29 20:20:53 +00:00
|
|
|
**Argument:** Number of characters that link text should be truncated to,
|
2010-12-28 22:53:28 +00:00
|
|
|
including the ellipsis that's added if truncation is necessary.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|urlizetrunc:15 }}
|
|
|
|
|
2008-03-18 04:21:08 +00:00
|
|
|
If ``value`` is ``"Check out www.djangoproject.com"``, the output would be
|
2010-12-28 22:53:28 +00:00
|
|
|
``'Check out <a href="http://www.djangoproject.com"
|
2018-08-21 13:28:51 +00:00
|
|
|
rel="nofollow">www.djangoproj…</a>'``.
|
2010-12-28 22:53:28 +00:00
|
|
|
|
|
|
|
As with urlize_, this filter should only be applied to plain text.
|
2008-03-18 00:14:41 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: wordcount
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``wordcount``
|
|
|
|
-------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Returns the number of words.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-01-12 23:36:19 +00:00
|
|
|
|
|
|
|
{{ value|wordcount }}
|
|
|
|
|
|
|
|
If ``value`` is ``"Joel is a slug"``, the output will be ``4``.
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: wordwrap
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``wordwrap``
|
|
|
|
------------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2005-11-06 23:30:06 +00:00
|
|
|
Wraps words at specified line length.
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2006-03-02 05:53:58 +00:00
|
|
|
**Argument:** number of characters at which to wrap the text
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
{{ value|wordwrap:5 }}
|
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
If ``value`` is ``Joel is a slug``, the output would be:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2008-03-18 00:14:41 +00:00
|
|
|
|
|
|
|
Joel
|
|
|
|
is a
|
|
|
|
slug
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. templatefilter:: yesno
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``yesno``
|
|
|
|
---------
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2015-04-14 08:43:14 +00:00
|
|
|
Maps values for ``True``, ``False``, and (optionally) ``None``, to the strings
|
|
|
|
"yes", "no", "maybe", or a custom mapping passed as a comma-separated list, and
|
2005-11-06 23:30:06 +00:00
|
|
|
returns one of those strings according to the value:
|
2005-07-17 15:15:26 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2010-09-26 20:52:36 +00:00
|
|
|
|
|
|
|
{{ value|yesno:"yeah,no,maybe" }}
|
|
|
|
|
2015-04-14 08:43:14 +00:00
|
|
|
========== ====================== ===========================================
|
2005-11-06 23:30:06 +00:00
|
|
|
Value Argument Outputs
|
2015-04-14 08:43:14 +00:00
|
|
|
========== ====================== ===========================================
|
2011-11-12 18:47:51 +00:00
|
|
|
``True`` ``yes``
|
2005-11-06 23:30:06 +00:00
|
|
|
``True`` ``"yeah,no,maybe"`` ``yeah``
|
|
|
|
``False`` ``"yeah,no,maybe"`` ``no``
|
|
|
|
``None`` ``"yeah,no,maybe"`` ``maybe``
|
2015-04-14 08:43:14 +00:00
|
|
|
``None`` ``"yeah,no"`` ``no`` (converts ``None`` to ``False``
|
|
|
|
if no mapping for ``None`` is given)
|
|
|
|
========== ====================== ===========================================
|
2006-06-04 00:58:39 +00:00
|
|
|
|
2011-11-18 13:01:06 +00:00
|
|
|
Internationalization tags and filters
|
2016-01-03 10:56:22 +00:00
|
|
|
=====================================
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
Django provides template tags and filters to control each aspect of
|
2011-12-24 11:58:49 +00:00
|
|
|
:doc:`internationalization </topics/i18n/index>` in templates. They allow for
|
2011-11-18 13:01:06 +00:00
|
|
|
granular control of translations, formatting, and time zone conversions.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``i18n``
|
|
|
|
--------
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
This library allows specifying translatable text in templates.
|
|
|
|
To enable it, set :setting:`USE_I18N` to ``True``, then load it with
|
|
|
|
``{% load i18n %}``.
|
|
|
|
|
|
|
|
See :ref:`specifying-translation-strings-in-template-code`.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``l10n``
|
|
|
|
--------
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
This library provides control over the localization of values in templates.
|
2023-01-06 13:46:33 +00:00
|
|
|
You only need to load the library using ``{% load l10n %}``.
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
See :ref:`topic-l10n-templates`.
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``tz``
|
|
|
|
------
|
2011-11-18 13:01:06 +00:00
|
|
|
|
|
|
|
This library provides control over time zone conversions in templates.
|
|
|
|
Like ``l10n``, you only need to load the library using ``{% load tz %}``,
|
|
|
|
but you'll usually also set :setting:`USE_TZ` to ``True`` so that conversion
|
|
|
|
to local time happens by default.
|
|
|
|
|
|
|
|
See :ref:`time-zones-in-templates`.
|
|
|
|
|
|
|
|
Other tags and filters libraries
|
2016-01-03 10:56:22 +00:00
|
|
|
================================
|
2006-06-04 00:58:39 +00:00
|
|
|
|
|
|
|
Django comes with a couple of other template-tag libraries that you have to
|
2011-05-29 17:41:04 +00:00
|
|
|
enable explicitly in your :setting:`INSTALLED_APPS` setting and enable in your
|
2011-10-03 08:06:01 +00:00
|
|
|
template with the :ttag:`{% load %}<load>` tag.
|
2006-06-04 00:58:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``django.contrib.humanize``
|
|
|
|
---------------------------
|
2006-06-04 00:58:39 +00:00
|
|
|
|
|
|
|
A set of Django template filters useful for adding a "human touch" to data. See
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`/ref/contrib/humanize`.
|
2006-06-04 00:58:39 +00:00
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``static``
|
|
|
|
----------
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
.. templatetag:: static
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``static``
|
|
|
|
~~~~~~~~~~
|
2011-06-30 09:06:19 +00:00
|
|
|
|
2011-08-11 14:07:39 +00:00
|
|
|
To link to static files that are saved in :setting:`STATIC_ROOT` Django ships
|
2015-11-07 11:24:38 +00:00
|
|
|
with a :ttag:`static` template tag. If the :mod:`django.contrib.staticfiles`
|
|
|
|
app is installed, the tag will serve files using ``url()`` method of the
|
2023-02-09 15:48:46 +00:00
|
|
|
storage specified by ``staticfiles`` in :setting:`STORAGES`. For example:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
{% load static %}
|
2020-11-02 09:34:24 +00:00
|
|
|
<img src="{% static 'images/hi.jpg' %}" alt="Hi!">
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
It is also able to consume standard context variables, e.g. assuming a
|
2023-02-09 15:48:46 +00:00
|
|
|
``user_stylesheet`` variable is passed to the template:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
{% load static %}
|
2022-01-21 14:46:37 +00:00
|
|
|
<link rel="stylesheet" href="{% static user_stylesheet %}" media="screen">
|
2011-06-30 09:06:19 +00:00
|
|
|
|
2012-07-07 13:30:25 +00:00
|
|
|
If you'd like to retrieve a static URL without displaying it, you can use a
|
2023-02-09 15:48:46 +00:00
|
|
|
slightly different call:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2012-07-07 13:30:25 +00:00
|
|
|
|
|
|
|
{% load static %}
|
|
|
|
{% static "images/hi.jpg" as myphoto %}
|
2018-01-21 07:09:10 +00:00
|
|
|
<img src="{{ myphoto }}">
|
2012-07-07 13:30:25 +00:00
|
|
|
|
2015-11-07 11:24:38 +00:00
|
|
|
.. admonition:: Using Jinja2 templates?
|
|
|
|
|
|
|
|
See :class:`~django.template.backends.jinja2.Jinja2` for information on
|
|
|
|
using the ``static`` tag with Jinja2.
|
2011-08-11 14:07:39 +00:00
|
|
|
|
2011-06-30 09:06:19 +00:00
|
|
|
.. templatetag:: get_static_prefix
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``get_static_prefix``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2011-06-30 09:06:19 +00:00
|
|
|
|
2014-01-17 20:58:19 +00:00
|
|
|
You should prefer the :ttag:`static` template tag, but if you need more control
|
|
|
|
over exactly where and how :setting:`STATIC_URL` is injected into the template,
|
2023-02-09 15:48:46 +00:00
|
|
|
you can use the :ttag:`get_static_prefix` template tag:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
{% load static %}
|
2018-01-21 07:09:10 +00:00
|
|
|
<img src="{% get_static_prefix %}images/hi.jpg" alt="Hi!">
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
There's also a second form you can use to avoid extra processing if you need
|
2023-02-09 15:48:46 +00:00
|
|
|
the value multiple times:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
{% load static %}
|
|
|
|
{% get_static_prefix as STATIC_PREFIX %}
|
|
|
|
|
2018-01-21 07:09:10 +00:00
|
|
|
<img src="{{ STATIC_PREFIX }}images/hi.jpg" alt="Hi!">
|
|
|
|
<img src="{{ STATIC_PREFIX }}images/hi2.jpg" alt="Hello!">
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
.. templatetag:: get_media_prefix
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``get_media_prefix``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~
|
2011-06-30 09:06:19 +00:00
|
|
|
|
|
|
|
Similar to the :ttag:`get_static_prefix`, ``get_media_prefix`` populates a
|
2023-02-09 15:48:46 +00:00
|
|
|
template variable with the media prefix :setting:`MEDIA_URL`, e.g.:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
2011-06-30 09:06:19 +00:00
|
|
|
|
2015-03-05 14:13:56 +00:00
|
|
|
{% load static %}
|
|
|
|
<body data-media-url="{% get_media_prefix %}">
|
|
|
|
|
|
|
|
By storing the value in a data attribute, we ensure it's escaped appropriately
|
|
|
|
if we want to use it in a JavaScript context.
|