1 templates:
What is a template?
html+ template Syntax
Templates include variables that are replaced by values when they are used, and tags that control template logic.
2 template syntax:
1 variables: {{}}
Deep query: Through the period symbol. Filter Filter{{var|filter_name}}
2 Tags: {% tag%}
Forif Else
For label
To iterate through each of the elements:
{% for who in person_list%} <p>{{person.name}}</p>{% endfor%}
You can use the {% for obj in list reversed %} reverse to complete the loop.
Traverse a dictionary:
{% for key,val in dic.items%} <p>{{Key}}:{{val}}</p>{% endfor%}
For ... empty
The for tag has an optional {% empty %} clause so that it can be manipulated when the given group is empty or not found.
{% for who in person_list%} <p>{{person.name}}</p>{% Empty%} <p>sorry,no person here</p>{% endfor%}
If label
{% if %} evaluates a variable, and if its value is "True" (exists, is not NULL, and is not a Boolean false value), the corresponding content block is output.
{% if num > or num < 0} <p> invalid </p>{% elif num > and num <%} <p> excellent </p>{% Else%} <p> Live it </P&G T {% ENDIF%}
With
Using a simple name to cache a complex variable is useful when you need to use an "expensive" method (such as accessing a database) many times.
For example:
{% with total=business.employees.count%} {{Total}} employee{{total|pluralize}}{% endwith%}
Csrf_token
This tag is used for cross-site request forgery protection
3 Customizing filters and Labels
Step: 1 in Settings in Installed_apps, whether there is a current application 2 create a TEMPLATETAGS3 in the app to create a py file in Templatetags: Mytag.py4 in mytag.py: Introduction: From Django Import templatefrom django.utils.safestring Import mark_saferegister = template. Library () #register的名字是固定的, cannot be changed 5 define filters and labels @register.filterdef multi (x, y): Return x*y@register.simple_tagdef Multitag ( X, Y, z): Return x*y*z6 import in the reference template: {% load MyTag%}7 Call: Filter: {{var|filter_name: parameter}} # parameter can only be a label: {% simple_tag parameter 1 parameter 2 ...%}
To compare filters with custom labels use:
Filter function can only have two parameters, no parameter limit for custom labels
Filters can be used in conjunction with logical tags, such as if tags. The custom label is not available.
Templates for the Django web framework