The road to WeChat applet development (3) Django template tags (variables & filters)

The road to WeChat applet development (3) Application of Django framework learning template 2

view:{"HTML变量名" : "views变量名"}
HTML:{{变量名}}

HelloWorld/HelloWorld/views.py file code:
Insert picture description here
runoob.html in templates:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>中国矿业大学计算机信息安全专业</title>
</head>
<body>
    <p>{
   
   { name }}</p>
</body>
</html>

Visit http://127.0.0.1:8000/runoob again, you can see the page
Insert picture description here
filter
template syntax:

{
   
   { 变量名 | 过滤器:可选参数 }}

The template filter can modify the variable before it is displayed. The filter uses pipe characters, as shown below:

{
   
   { name|lower }}

After the { {name }} variable is processed by the filter lower, the uppercase of the document is converted to lowercase.
The filter pipeline can be *socketed*, which means that the output of one filter pipeline can also be used as the input of the next pipeline:

{
   
   { my_list|first|upper }}

The above example takes the first element and converts it to uppercase.
Some filters have parameters. The filter parameters follow the colon and are always enclosed in double quotes. E.g:

{
   
   { bio|truncatewords:"30" }}

This will display the first 30 words of the variable bio.
Other filters:

addslashes : 添加反斜杠到任何反斜杠、单引号或者双引号前面。
date : 按指定的格式字符串参数格式化 date 或者 datetime 对象,实例:
{
   
   { pub_date|date:"F j, Y" }}
length : 返回变量的长度。

default

default provides a default value for the variable.

If the boolean value of the variable passed by views is false, the specified default value is used.

The following values ​​are false:

0  0.0  False  0j  ""  []  ()  set()  {}  None

length
returns the length of the object for a list of strings and
the dictionary returns the number of key-value pairs, the length is set to return to the re
filesizeformat
display size of the file in a more readable manner (i.e. '13 KB ',' 4.1 MB', '102 bytes', etc.) The
dictionary returns the number of key-value pairs, and the collection returns the length after deduplication.
Insert picture description here
date
formats a date variable according to a given format.
The format Ymd H:i:s returns the time in the format of year-month-day hour:minute:second.
Insert picture description here
truncatechars
If the total number of characters in the string is more than the specified number of characters, the latter part will be truncated.
The truncated string will end with...

Guess you like

Origin blog.csdn.net/xulei1132562/article/details/113557754