Template Tags and Filters¶
context_tags
module¶
Use {% load context_tags %}
in your template to load this module.
Sets (updates) current template context with the rendered output of the block inside tags.
This is useful when some template tag does not support storing its output in the context itself or we need some complex content (like language, user or URL dependent content) multiple times.
Example usage:
{% setcontext as varname %} {% complextag %} {% endsetcontext %}
lang_tags
module¶
Use {% load lang_tags %}
in your template to load this module.
Translates given string to the specified language.
This is useful for text you need in some other language than the current language. For example, for links inviting users to switch to their language.
Example usage:
{% translate "Do you understand this?" "de" %}
list_tags
module¶
Use {% load list_tags %}
in your template to load this module.
Divides input list into the given number of sublists.
Last sublist can be shorter if input list length is not a multiplier of the given number of sublists.
Example usage:
<tr> {% for column in objects|divide_list:"2" %} <td><ul> {% for obj in column %} <li>{{ obj }}</li> {% endfor %} </ul></td> {% endfor %} </tr>
Splits input list into sublists of the given length.
Last sublist can be shorter if input list length is not a multiplier of the given length.
Example usage:
{% for group in objects|split_list:"4" %} <tr> {% for obj in group %} <td>{{ obj }}</td> {% endfor %} </tr> {% endfor %}
string_tags
module¶
Use {% load string_tags %}
in your template to load this module.
Returns the number of non-overlapping occurrences of an argument substring in the given string.
Ensures that string ends with dot if it does not already end with some punctuation.
Returns True if the given string starts with an argument prefix, otherwise returns False.
url_tags
module¶
Use {% load url_tags %}
in your template to load this module.
Builds an absolute (full) URL from the given location and the variables available in the request.
If no location is specified, the absolute (full) URL is built on
django.http.HttpRequest.get_full_path()
.It is a wrapper around
django.http.HttpRequest.build_absolute_uri()
. It requiresrequest
to be available in the template context (for example, by usingdjango.core.context_processors.request
context processor).Example usage:
{% url "view_name" as the_url %} {% fullurl the_url %}
Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens.
It is similar to built-in
slugify
but it also handles special characters in variety of languages so that they are not simply removed but properly transliterated/downcoded.
Creates URI template in a similar way to how
url
tags work but leaving parts of a URI to be filled in by a client. See RFC 6570 for more information.Names of parts are taken from named groups in URL regex pattern used for the view, or as a part’s sequence number (zero-based) for unnamed groups. You can pre-fill some parts by specifying them as additional arguments to the tag.
Warning
Tag cannot check if pre-fill values specified will really match back the URL regex pattern, so make sure yourself that they do.
Example usage:
{% with variable="42" %} {% urltemplate "view_name" arg1="value" arg2=variable %} {% endwith %}
If URL pattern would be defined like:
url(r'^some/view/(?P<arg1>.*)/(?P<arg2>.*)/(?P<param>.*)/$', some_view, name='view_name'),
The result would be:
/some/view/value/42/{param}/
Note
Requires Django 1.4+.