# # Template variables
-Normal variable
{{Name}}
-Object variables (use point numbers to access object properties and methods, method without parentheses)
{{Person.name}}
# # Common Template Tags
# if tag, supports and,or,not and nesting, does not support parentheses
{% if%} {% ENDIF%}
{% if%} {% Elif%} ... {% Else%} {% ENDIF%}
# for tag, usage is basically the same as in Python
{% for item in items%} {% ENDFOR%}
{% for item in items reversed%} {% ENDFOR%} # Reverse Iteration
{% for K, V in data.items%} {% ENDFOR%}
{% for item in items%} #相当于: if items:
Statement A for item in items:
{% empty%} statement one
Statement two else:
{% ENDFOR%} statement two
-Bring the Forloop object in the for label
-Forloop.counter counting starting from 1
{% for item in items%}
{{Forloop.counter}}: {{item}}
-Forloop.counter0 counting starting from 0
-Forloop.revcounter inverse count, the last number is 1
-Forloop.revcounter0 inverse count, the last number is 0
-Forloop.first Boolean value, True for first time loop
-Forloop.last Boolean value, True for Last loop
-Forloop.parentloop Forloop object referencing the parent loop
# Equal judgment (x, y can be a variable, string, Integer, decimal, variable does not need to add {{}}, the string can be a single/double quotation mark caused by hard coding, but the list, dictionary, Boolean value can not be hard-coded, the tag variable may be assigned, and also support {% else%} label)
{% ifequal x y%} {% endifequal%}
{% ifnotequal x y%} {% endifnotequal%}
# Notes
{# single-line Comment #}
{% Comment%}
Multi-line comments
{% Endcomment%}
# # filter (Python is basic for string methods)
{{Name|lower}} full lowercase
{{Name|upper}} All Caps
{{Name|title}} Capitalize first letter
The first element of {{Users_list|first}}}
{{Users_list|last}} last Element
{{sentence|truncatewords: ' 5 '}} string truncation, separated by a space (so Chinese is invalid), take the first five words, superfluous words with ... Omitted, numbers must be enclosed in double quotes
{{sentence|slice: "4"}} string segmentation, can be used in Chinese, redundant words without any identification
{{sentence|addslashes}} in backslash, single quote, double quotation mark preceded by a slash, can be used to escape the string
{{birthday|data: "F J, Y"}} formats a DATE or Datatime object, or it can be date_format, Datetime_format, Short_date_format, or short_ in double quotes Date-time_format
{{Word:length}} return length
# # Template reference, referencing the entire contents of other templates
{% include URL%} # URL can be a double-quote string hard-coded, or it can be a variable
# # Template Inheritance
-The parent template uses a block tag to represent blocks that can be modified by a quilt template
{% block Block_name%} {% Endblock%}
-The child template inherits the parent template in the first line of the file using the extends tag, and also modifies the block with the same name in the parent template using the block tag
{% extends URL%} # URL can be a double-quote string hard-coded, or it can be a variable
{% block block_name%} New content {% Endblock%}
-Any template in the inheritance tree can access every template variable in the context
# # using templates
1 fromDjango.shortcutsImportRender2 3 4 defHome (Request):5 returnRender (Request,"index.html", {"name":"Stanley"," Age":" A"})6 #The first parameter must be request, the second parameter is the template path, and the third argument is the template variable
Django Template language (DTL) basics