When a developer develops templates, he/she regularly needs to use the template language and filters.
The following are the key elements of the template language:
{% autoescape on OR off %} {% endautoescape %}
: This automatically starts the auto-escape feature that helps protect the browser of the displayed data (XSS).{% block block_name %} {% endblock %}
: This sets the blocks that can be filled by templates that inherit from them.{% comment %} {% endcomment %}
: This sets a comment that will not be sent to the user as HTML.{% extends template_name %}
: This overrides a template.{% spaceless %}
: This removes all the whitespaces between the HTML tags.{% include template_name %}
: This includes a template namedtemplate_name
in the current template. The blocks included templates that cannot be redefined.
This section shows you how to loop through a dictionary. The steps involved in looping are as follows:
{% for var in list_var %}
: This allows looping in thelist_var
dictionary{% empty %}
: This displays the subsequent code if the dictionary is empty{% endfor %}
: This indicates the end of a loop
This section shows how to execute a conditional statement:
{% if cond %}
: This line checks the condition and discusses the following code when enabled.{% elif cond %}
: This line checks another condition if the first has not been verified. If this condition is satisfied, the following code will be processed.{% else %}
: This line will process the following code if none of the previous conditions have been validated.
The following are the different template filters:
title
: This capitalizes all the first characters of each wordcut
: This removes all the values of the argument from the given string, for example,{{ value|cut:"*" }}
removes all the*
characterslinebreaks
: This replaces line breaks in text with the appropriate HTML tagsdate
: This displays a formatted date, for example,{{ value|date:"D d M Y" }}
will displayWed 09 Jan 2008
pluralize
: This allows you to display plurals, shown as follows:You have {{ nb_products }} product{{ nb_products|pluralize }} in our cart. I received {{ nb_diaries }} diar{{ nb_diaries|pluralize:"y,ies" }}.
linenumbers
: This displays text with line numbers at the left-hand sideescapejs
: This escapes characters to use in JavaScript stringsdefault
: This defines a default value if the original value equalsNone
orempty
; for example, with{{ value|default:"nothing" }}
, if the value is""
, it will displaynothing
.dictsort
: This sorts the dictionary in the ascending order of the key; for example,{{ value|dictsort:"price"}}
will sort the dictionary byprice
dictsortreversed
: This is used to sort the dictionary in the descending order of the keyfloatformat
: This formats a float value, and the following are the examples:When 45.332
is the value,{{ value|floatformat:2 }}
displays45.33
When
45.00
is the value,{{ value|floatformat:"-2" }}
displays45
The following are the queryset methods:
filter(condition)
: This method allows you to filter a queryset.none()
: This method can return an empty queryset. This method is useful when you want to empty a queryset.dinstinct(field_name)
: This method is used to retrieve the unique values of a field.values_list(field_name)
: This method is used to retrieve the data dictionary of a field.get(condition)
: This method is used to retrieve a record from a model. When using this method, you must be sure that it concerns only one record.exclude(condition)
: This method allows you to exclude some records.
The following elements are the aggregation methods: