Django Template Tags
If/else Label
The basic syntax format is as follows:
{% if condition%}
... display
{% ENDIF%}
Or:
{% if condition1%}
... display 1
{% elif Condiiton2%}
... display 2
{% Else%}
... display 3
{% ENDIF%}
Determine whether the output is based on conditions. If/else supports nesting.
{% if%} tags accept and, or or not keyword to make judgments on multiple variables, or to negate (not) variables, for example:
{% if athlete_list and coach_list%}
Both the athletes and coaches variables are available.
{% ENDIF%}
For label
{% for%} allows us to iterate over a sequence.
Similar to the case for a python for statement, the looping syntax is for X in Y, Y is the sequence to iterate and X is the name of the variable used in each particular loop.
In each iteration, the template system renders all content between {% for%} and {% endfor%}.
For example, given an athlete list athlete_list variable, we can use the following code to display this list:
<ul>
{% for athlete in athlete_list%}
<li>{{Athlete.name}}</li>
{% ENDFOR%}
</ul>
Adding a reversed to the label causes the list to be reversed:
{% for athlete in athlete_list reversed%}
...
{% ENDFOR%}
You can nest using the {% for%} tag:
{% for athlete in athlete_list%}
<ul>
{% for sport in athlete.sports_played%}
<li>{{Sport}}</li>
{% ENDFOR%}
</ul>
{% ENDFOR%}
Ifequal/ifnotequal Label
The {% ifequal%} label compares two values and displays all values in {% ifequal%} and {% endifequal%} when they are equal.
The following example compares two template variables of user and CurrentUser:
{% ifequal user CurrentUser%}
{% endifequal%}
Similar to {% if%}, {% ifequal%} supports optional {% else%} Tags: 8
{% ifequal section ' sitenews '%}
{% Else%}
{% endifequal%}
Comment Label
The Django comment uses {# #}.
{# This is a comment #}
Filter filters
The template filter can modify the variable before it is displayed, and the filter uses the pipe character as follows:
{{Name|lower}}
After the {{name}} variable is processed by the filter lower, the document capitalization conversion text is lowercase.
The filter pipe can be * socket *, which is to say, the output of a filter pipe can be used as input to the next pipe:
{{My_list|first|upper}}
The above example converts the first element and converts it to uppercase.
Some filters have parameters. The parameters of the filter follow the colon and are always enclosed in double quotation marks. For example:
{{bio|truncatewords: ' 30 '}}
This will show the first 30 words of the variable bio.
Other filters:
Addslashes: Adds a backslash to any backslash, single quotation mark, or double quotation mark before.
Date: Formats a date or DateTime object by the specified format string parameter, instance:
{{pub_date|date: "F J, Y"}}
Length: Returns the lengths of the variables.
Include tags
The {% include%} tag allows the contents of other templates to be included in the template.
The following two examples all include the nav.html template:
{% include "nav.html"%}
Template inheritance
Templates can be reused in a way that inherits.
Next we create the templates directory of the previous project to add the base.html file, the code is as follows:
<title>hello world!</title>
<body>
{% block mainbody%}
<p>original</p>
{% Endblock%}
</body>
In the above code, the block tag named Mainbody is the part that can be replaced by the successor.
All {% block%} tags tell the template engine that child templates can overload these parts.
Hello.html inherits the base.html and replaces the specific block,hello.html modified code as follows:
{% extends "base.html"%}
{% block mainbody%}
<p> inherited the base.html file </p>
{% Endblock%}
The first line of code illustrates that hello.html inherits the base.html file. As you can see, the block tag of the same name here replaces the corresponding block of base.html.
Re-access address http://127.0.0.1:8000/hello/, the output is as follows:
Django template tags [go]