Browse Catalogs
First, label TAGS1, common variables
- Normal variable with {{}}
- Variable names consist of numbers, letters, underscores
- Point. Used in template language to get the corresponding property value of an object
Example:
{# takes the first argument in variable #} {{variable.0}} {# Take the value of key in the Dictionary dic #} {{Dic.key}} {# takes the Attr property value of the first object in the Obj_list object list #} {{obj_list.0.attr}} {# Point operation can only invoke a method with no parameters #} {{Obj_list.0.method}}
2. Logical Judgment
Logical judgment with {%%}
For
Variable |
Description |
Forloop.counter |
Index value of the current loop (starting at 1) |
Forloop.counter0 |
Index value of the current loop (starting at 0 |
Forloop.revcounter |
Reverse index value of the current loop (starting at 1) |
Forloop.revcounter0 |
Reverse index value of the current loop (starting at 0) |
Forloop.first |
The current loop is not the first loop (Boolean value) |
Forloop.last |
The current loop is not the last loop (Boolean value) |
Forloop.parentloop |
Outer loop of this layer loop |
If
With
{% with new_variable = old_variable%} ..... {% Endwith%}
- Role: Define intermediate variables
Csrf_token
{% Csrf_token%}
Comments
Grammar:
{# comment Content #}
3. Master Board System inheritance
<! DOCTYPE html>
{% extends ' base.html '%}
- Function: Use the above syntax to inherit the motherboard at the top of the page in a sub-page
Blocks block
{% block Block_name%} ... {% Endblock%}
- function: replace the corresponding content in the motherboard by defining the block name in the motherboard in the sub-page
Example:
{% block Page-main%} <p> Thin </p> <p> human evil </p> <p> rain Send dusk flower </p>{% endblock%}
Component
{% include ' subassembly.html '%}
- Function: You can save commonly used fixed HTML content in a separate file, where needed to import using the above syntax
4. Static file Related
In Settings, you set up the static statically folder and import it in HTML.
Grammar:
{% load static%}
For example:
{% load static%}
When referencing a JS file, use:
{% load static%}<script src= "{% static" Mytest.js "%}" ></script>
A file is used in many places to be saved as a variable
{% load static%} {% static "images/hi.jpg" as Myphoto%}</img>
Using Get_static_prefix
{% load static%}
Or
{% load static%} {% Get_static_prefix as Static_prefix%}
5. Custom Simpletag
From django Import templateregister = template. Library () @register. Simple_tag (name= "My_simpletag") def Add_simpletag (a,b,c): # can receive multiple parameters return ' {} + {} + {} '. Format (A, B, C
-
- Before using custom Simple_tag, first import the py file in the HTML page
{% load Custom_simpletag%} {% my_simpletag "1" "2" "3"%}
6. Custom Inclusion_tag
From django Import templateregister = template. Library () @register. Inclusion_tag (' inclusion_tag.html ') def my_inclusiontag (n): n = 1 if n < 1 else int (n) data = ["{} key". Format (i) for I in range (1, n+1)]return {"Data": Data}
-
- Create the inclusion_tag.html file that you just registered in the Templates folder
<ul> {% for choice in data%} <li>{{choice}}</li> {% endfor%}</ul>
-
- Before using custom My_inclusiontag, first import the py file in the HTML page
{% load Custom_inclusiontag%} {% My_inclusiontag 10%}
Second, filter Filter1, built-in filter
Grammar:
{{Value|filter_name:args}}
Note: There are no spaces before and after pipe characters
Default
{{value:default: ' Custom Content '}}
- Function: Displays the custom content if value is not passed
Length
{{Value|length}}
- Function: Returns the length of value, such as value=[' A ', ' B ', ' C ', ' d '], which shows 4.
Filesizeformat
{{Value|filesizeformat}}
- Function: Format value as a "human readable" file size format
If value is 123456789, the output will be 117.7 MB.
Slice
{{value|slice: ' Start:end '}}
Date
{{value|date: "Y-m-d h:i:s"}}
- Function: Custom format value for time format
Safe
{{Value|safe}}
- Function: Cancels the automatic escape of Django syntax tags such as HTML tags and js, thinking that inserting HTML or JS is safe and does not have to be escaped
Truncatechars
{{Value|truncatechars:num}}
- Action: If the string character is more than the specified number of characters, it is truncated. The truncated string ends with a translatable sequence of ellipses ("..."). Num parameter refers to the number of characters truncated
2. Custom Filter Steps
From django Import templateregister = template. Library () @register. Filter (name= "My_filter") def my_filter (value, arg): #最多接收两个参数return value.replace (ARG, "")
- Before using a custom filter, first import the py file in the HTML page
{% load Custom_filter%} {{variable|my_filter: ' 0 '}}
Template language for the Django framework