Template Tags and Filters¶
django-adminlte2-templates provides several template tags and filters that can be used by adding {% load adminlte2_tags %}
to your layout templates:
Tags¶
-
{% add_active %}
Add HTML class name
active
to sidebar and header navigation links based on comparison of given URL pattern with current URL.Parameters:
- url_pattern:
- (str) URL pattern for
reverse
matching or raw URL string. - exact_match:
- (bool, optional) Toggle for exact matching, defaults to
False
. - not_when:
- (str, optional) Comma-separated string of patterns that will render comparison as
False
, defaults to''
. - *args and **kwargs
- For URL
reverse
matching.
Example:
<li class="{% add_active 'app:page' object.pk %}"> <a href="{% url 'app:page' object.pk %}">Details</a> </li>
-
{% gravatar_url %}
Generates a Gravatar URL based on the details of the current user.
For your convenience, you can also set the parameters related to this tag in Django
settings.py
. Please check the Settings > Gravatar section for more information.To know more about Gravatar request parameters, please check the Gravatar Image Request documentation for more information.
Parameters:
- user:
- (User object, optional) User object, defaults to context
user
. - size:
- (int, optional) Image size. You may request images anywhere from
1
up to2048
, however note that many users have lower resolution images, so requesting larger sizes may result in pixelation/low-quality images. Defaults to80
. - default:
- (str, optional) Default Gravatar image to load. You can supply your own default image by supplying the URL to an image.
Alternatively, you can use any of these valid values:
'404'
,'mp'
,'identicon'
,'monsterid'
,'wavatar'
,'retro'
,'robohash'
,'blank'
. Defaults to'mp'
. - force_default:
- (bool, optional) Toggle to force load default Gravatar image, defaults to
False
. - rating:
- (str, optional) Gravatar image rating. Valid values are:
'g'
,'pg'
,'r'
,'x'
. Defaults to'pg'
.
Example:
<img src="{% gravatar_url %}" alt="User Avatar">
-
{% paginator %}
Generates an HTML code block for ListView pagination. HTML code output is based on template layout
adminlte2/extras/paginator.html
.You can customize the output by overriding the aforementioned layout and using the template blocks related to this tag. Please check the Template Blocks > Extras > Paginator section for more information.
Parameters:
- adjacent_pages
- (int, optional) Adjacent page links to current page link, defaults to
2
. - align
- (str, optional) Element alignment. Valid values are
'initial'
,'center'
,'left'
,'right'
, defaults to'initial'
. - no_margin
- (bool, optional) Toggle to remove margin around element, defaults to
False
.
Example:
{% if is_paginated %} {% paginator adjacent_pages=2 align="center" no_margin=True %} {% endif %}
-
{% page_title %}
Generates text for HTML <title> tag. Supports Django
sites
framework andListView
pagination.You can customize the output by using the settings related to this tag in Django
settings.py
. Please check the Settings > Page Title section for more information.Parameters:
- page_name
- (str, optional) Page title text. Adding
page_name
to the page context will override this parameter. Defaults to''
.
Example:
{% block title %}{% page_title 'Page Title' %}{% endblock title %}
Filters¶
-
add_class
Add HTML class names to a form field.
Example:
{% for field in form %} <div class="form-group {% if field.errors %}has-error{% endif %}"> {{ field.label_tag }} {% if field.errors %} <div class="text-danger"> {{ field.errors }} </div> {% endif %} {{ field|add_class:'form-control' }} {% if field.help_text %} <p class="help-block">{{ field.help_text|safe }}</p> {% endif %} </div> {% endfor %}
-
add_attr
Add HTML attribute values to a form field.
Example:
{% for field in form %} <div class="form-group {% if field.errors %}has-error{% endif %}"> {{ field.label_tag }} {% if field.errors %} <div class="text-danger"> {{ field.errors }} </div> {% endif %} {% if field.is_readonly %} {{ field.field|add_attr:'readonly' }} {% else %} {{ field.field }} {% endif %} {% if field.help_text %} <p class="help-block">{{ field.help_text|safe }}</p> {% endif %} </div> {% endfor %}