A Django template is a simple text file that can generate any text format (HTML, XML, CSV, etc.), before we start this article, let's take a look at an example of a Django template:
As you can see from this example, template tags can appear alone, such as the extends tag; or they can appear in pairs, such as block tags and for tags. The main body of the template is a text file with HTML structure. I think people who have experience in ASP and PHP development will feel very friendly, because their syntax is very similar, and they are all markup languages embedded in HTML.
Variables
Basic usage of variables: {{ variable }}, when the template engine encounters a variable, it will replace the variable with a specific value. Variable names can contain alphanumerics and underscores, but cannot contain spaces and other special symbols. Periods (.) have special meaning in variables, and if the template engine encounters a period, it will interpret it in the following order:
dictionary lookup
Find properties and methods
Find subscripted element
Note that if the variable following the period is a method that can be called, then the method will be called with an empty parameter. For example, the iteritems method of a dictionary can be called in the template as follows:
Filters
You can use filters to modify the display style of variable values. How the filter is used {{ variable | filter method }}. Filters can be used consecutively in the form: {{ variable | filter method 1 | filter method 2 }}.
Note that there can be no spaces between variables, pipes (|) and filter methods.
Some filters can also receive parameters, for example: {{ bio|truncatewords:30 }}, this code means to display the first 30 words of bio.
If the filter parameter contains spaces, the parameter should be enclosed in quotes, for example: {{ list|join:", " }}.
Django provides about 60 filters, the specific introduction can be seen on the Django official website:
https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#ref-templates-builtins-filters。
Here are some commonly used filters:
Default
If the variable is false or empty, display the default value: for example: {{ value|default:"nothing" }}, if the value is false, nothing will be displayed on the page.
Length
Display the length of a string or array, such as: {{ value|length }}
Filesizeformat
Display the file size in human-readable form. For example, if a file is 123456789, it will be displayed as 117.7 MB. Syntax: {{ value|filesizeformat }}
Add
Addition: {{ value|add:"2" }}
This method will first calculate according to the value, and if it fails, it will directly concatenate the two values, such as concatenating two arrays.
Capfirst
Capitalize first letter: {{ value|capfirst }}
cut
Delete the specified value, for example, remove spaces in the string: {{ value|cut:" " }}
If value is "String with spaces", then output "Stringwithspaces".
Date
Formatting the date, it is also important to have more parameters in this method, please refer to the Django official website.
Dictsort
Returns a dictionary sorted by a dictionary item,
For example, there is a dictionary users as follows, containing three people information:
Sort by name:
Display style:
Escape
Convert an html string to:
The browser shows:
Join
splice multiple elements
Tags
For tag
Loop through the code inside the tag, for example:
If, elif and else tags
Like the if statement in other programming languages, a block of code is executed when the condition is true or not empty, for example:
If tags can be nested other tags.
Comments
Two annotation methods:
{# here is the comment#}
Annotation tags: