Go to Django TEMPLATE 1 and continue to write the template knowledge. If tag
{% If %}Tag acceptanceAnd,OrOrNotKeyword to judge multiple variables, or to reverse the variables (Not).
But it cannot be used in the same label at the same time.AndAndOr, the following is invalid:
{% if athlete_list and coach_list or cheerleader_list %}
It is okay to use the same logical operator multiple times, but we cannot combine different operators.
{% if athlete_list or coach_list or parent_list or teacher_list %}
The system does not support combination and comparison with parentheses. If you need to use parentheses to combine and express your logic, consider moving it out of the template and passing it in as a template variable. Or just use nested{% If %}Replace tags.
In addition, there is no{% Elif %}Tag (note that the python syntax and Django template syntax are distinguished). Use the nested '{% if %}' tag to achieve the same effect.
Finally, be sure to use{% Endif %}Close each{% If %}Label.
For tag
The {% for %} tag allows us to iterate over a sequence.
Given a list of AthletesAthlete_listVariable, we can use the following code to display this list
<ul>{% for athlete in athlete_list %} <li>{{ athlete.name }}</li>{% endfor %}</ul>
AddReversedThis causes the list to be reversely iterated.
{% for athlete in athlete_list reversed %}...{% endfor %}
It is a common practice to check the list size before executing a loop. when the list is empty, some special prompts are output.
{% if athlete_list %} {% for athlete in athlete_list %} <p>{{ athlete.name }}</p> {% endfor %}{% else %} <p>There are no athletes. Only computer programmers.</p>{% endif %}
Because this method is very common, the ''for ''tag supports an optional ''' {% empty %}'' clause, with this function, we can define the output content when the list is empty. The following example is equivalent to the previous one:
{% for athlete in athlete_list %} <p>{{ athlete.name }}</p>{% empty %} <p>There are no athletes. Only computer programmers.</p>{% endfor %}
In Django, The for tag does not support exit loop operations or the continue statement.
In each {% for %} loop, there is a template variable called 'forloop. This variable has some attributes that indicate the cyclic progress information,Forloop. CounterAlways an integer counter that indicates the number of executions of the current loop. This counter starts from 1, so during the first cycleForloop. CounterIt will be set to 1.
{% for item in todo_list %} <p>{{ forloop.counter }}: {{ item }}</p>{% endfor %}
Forloop. counter0SimilarForloop. CounterBut it is counted from 0.
Forloop. revcounterIs the integer variable that represents the remaining items in the loop.
Forloop. revcounter0SimilarForloop. revcounterBut it uses 0 as the end index.
Forloop. FirstIs a Boolean value. If this iteration is executed for the first time, it is set to true.
Forloop. LastIs a Boolean value. It is set to true during the last execution of the loop.
{% for link in links %}
{{ link }}
{% if not forloop.last %}
|
{% endif %}
{% endfor %}
Forloop. parentloopIs a previous loop pointing to the current loopForloopObject Reference (nested loop)
Ifequal/ifnotequal label
{% Ifequal %}Labels compare two values. When they are equal{% Ifequal %}And{% Endifequal %}All values in.
{% ifequal user currentuser %}
And{% If %}Similarly,{% Ifequal %}Optional{% Else %}Tags:
{% ifequal section ‘sitenews‘ %}
Only template variables, strings, integers, and decimals can be used{% Ifequal %}Tag parameters. Other types, such as the dictionary type, list type, and boolean type of Python, cannot be used in{% Ifequal %}Medium
{# okay below #}{% ifequal variable 1 %}{% ifequal variable 1.23 %}{% ifequal variable ‘foo‘ %}{% ifequal variable "foo" %}{# error below #}{% ifequal variable True %}{% ifequal variable [1, 2, 3] %}{% ifequal variable {‘key‘: ‘value‘} %}
Use{% If %}To replace{% Ifequal %}.
Comment tag
Single line comment usage{##}
{# This is a comment #}
Multi-line comment, you can use the {% comment %} template tag
{% comment %}This is amulti-line comment.{% endcomment %}
Filter
The filter uses pipeline characters (|)
Example: {name | Lower }}
The filter pipeline can be nested, that is, the output of a filter pipeline can be used as the input of the next pipeline. In the following example, the first element of the List is searched and converted to uppercase.
{{ my_list|first|upper }}
Some filters have parameters. The filter parameters follow the colon and are always enclosed in double quotation marks. For example:
{{ bio|truncatewords:"30" }}
This will show VariablesBioThe first 30 words.
Addslashes: Add a backslash to the front of any backslash, single quotation marks, or double quotation marks. This is useful when processing text containing JavaScript.
Date: Format by the specified format string parameter
DateOr
DatetimeObject. Example:
{{ pub_date|date:"F j, Y" }}
Length: Return the length of the variable. For the list, this parameter returns the number of list elements. For a string, this parameter returns the number of characters in the string. You can use this method for a list or string, or any Python object that knows how to determine the length (that is, there are_ Len __()Method object ).
Summary
In the Basic copy book, I picked a part of it myself. There were too many things and no tests were conducted. The following content is described in the running example.