django.template.Library.inclusion_tag()
Another common type of template tag is the type that displays some data by rendering another template. For example, Django’s admin interface uses custom template tags to display the buttons along the bottom of the “add/change” form pages. Those buttons always look the same, but the link targets change depending on the object being edited – so they’re a perfect case for using a small template that is filled with details from the current object. (In the admin’s case, this is the submit_row
tag.)
These sorts of tags are called “inclusion tags”.
Writing inclusion tags is probably best demonstrated by example. Let’s write a tag that outputs a list of choices for a given Poll
object, such as was created in the tutorials. We’ll use the tag like this:
{% show_results poll %}
...and the output will be something like this:
<ul> <li>First choice</li> <li>Second choice</li> <li>Third choice</li> </ul>
First, define the function that takes the argument and produces a dictionary of data for the result. The important point here is we only need to return a dictionary, not anything more complex. This will be used as a template context for the template fragment. Example:
def show_results(poll): choices = poll.choice_set.all() return {'choices': choices}
Next, create the template used to render the tag’s output. This template is a fixed feature of the tag: the tag writer specifies it, not the template designer. Following our example, the template is very simple:
<ul> {% for choice in choices %} <li> {{ choice }} </li> {% endfor %} </ul>
Now, create and register the inclusion tag by calling the inclusion_tag()
method on a Library
object. Following our example, if the above template is in a file called results.html
in a directory that’s searched by the template loader, we’d register the tag like this:
# Here, register is a django.template.Library instance, as before @register.inclusion_tag('results.html') def show_results(poll): ...
Alternatively it is possible to register the inclusion tag using a django.template.Template
instance:
from django.template.loader import get_template t = get_template('results.html') register.inclusion_tag(t)(show_results)
...when first creating the function.
Sometimes, your inclusion tags might require a large number of arguments, making it a pain for template authors to pass in all the arguments and remember their order. To solve this, Django provides a takes_context
option for inclusion tags. If you specify takes_context
in creating a template tag, the tag will have no required arguments, and the underlying Python function will have one argument – the template context as of when the tag was called.
For example, say you’re writing an inclusion tag that will always be used in a context that contains home_link
and home_title
variables that point back to the main page. Here’s what the Python function would look like:
@register.inclusion_tag('link.html', takes_context=True) def jump_link(context): return { 'link': context['home_link'], 'title': context['home_title'], }
Note that the first parameter to the function must be called context
.
In that register.inclusion_tag()
line, we specified takes_context=True
and the name of the template. Here’s what the template link.html
might look like:
Jump directly to <a href="{{ link }}">{{ title }}</a>.
Then, any time you want to use that custom tag, load its library and call it without any arguments, like so:
{% jump_link %}
Note that when you’re using takes_context=True
, there’s no need to pass arguments to the template tag. It automatically gets access to the context.
The takes_context
parameter defaults to False
. When it’s set to True
, the tag is passed the context object, as in this example. That’s the only difference between this case and the previous inclusion_tag
example.
inclusion_tag
functions may accept any number of positional or keyword arguments. For example:
@register.inclusion_tag('my_template.html') def my_tag(a, b, *args, **kwargs): warning = kwargs['warning'] profile = kwargs['profile'] ... return ...
Then in the template any number of arguments, separated by spaces, may be passed to the template tag. Like in Python, the values for keyword arguments are set using the equal sign (“=
”) and must be provided after the positional arguments. For example:
{% my_tag 123 "abcd" book.title warning=message|lower profile=user.profile %}
Please login to continue.