In the template , Sometimes you need to process some data before you can use it . Generally in Python We do this in the form of functions . And in the template , It's through filters . The filter uses | To use . For example, use add filter , So the sample code is as follows :
{{ value|add:"2" }}Here are the filters commonly used in development .
Add the parameter passed in to the original value . This filter will try to value and Parameters Convert to shape and add . If the conversion to plastic fails , It will be value and Parameters Splicing . If it's a string , Then it will be spliced into a string , If it's a list , Then it will be spliced into a list . The sample code is as follows :
{{ value|add:"2" }} If value Is equal to 4, So the result will be 6. If value Is equal to a normal string , such as abc, So the result will be abc2.add The source code of the filter is as follows :
def add(value, arg): """Add the arg to the value.""" try: return int(value) + int(arg) except (ValueError, TypeError): try: return value + arg except Exception: return ''
Remove all specified strings from the value . Be similar to python Medium replace(args,""). The sample code is as follows :
{{ value|cut:" " }} The above example will remove value All the space characters in .cut The source code of the filter is as follows :
def cut(value, arg): """Remove all values of arg from the given string.""" safe = isinstance(value, SafeData) value = value.replace(arg, '') if safe and arg != ';': return mark_safe(value) return value
Put a date in the specified format , Format as a string . The sample code is as follows :
# data
context = {
"birthday": datetime.now()
}
# Template
{{ birthday|date:"Y/m/d" }} Then it will output 2018/02/01. among Y Represents a four digit year ,m Represents a two digit month ,d Represents a two digit day . There's more time for formatting . See the table below .
Format characters
describe
Example
Y
Four digit year
2018
m
Two digit months
01-12
n
month ,1-9 There is no 0 Prefix
1-12
d
Two digit days
01-31
j
God , however 1-9 There is no 0 Prefix
1-31
g
Hours ,12 Hour format ,1-9 There is no 0 Prefix
1-12
h
Hours ,12 Hour format ,1-9 There is 0 Prefix
01-12
G
Hours ,24 Hour format ,1-9 There is no 0 Prefix
1-23
H
Hours ,24 Hour format ,1-9 There is 0 Prefix
01-23
i
minute ,1-9 There is 0 Prefix
00-59
s
second ,1-9 There is 0 Prefix
00-59
If the value is evaluated as False. such as [],"",None,{} Wait for this if In judgment, it is False Value , Will use default The default value provided by the filter . The sample code is as follows :
{{ value|default:"nothing" }} If value Is equal to an empty string . such as "", Then the above code will output nothing.
If the value is None, Then it will use default_if_none Default value provided . This and default There's a difference ,default It's all assessed as False Will use the default value . and default_if_none Only this value is equal to None The default value is used . The sample code is as follows :
{{ value|default_if_none:"nothing" }} If value Is equal to "" That is, an empty string , So the above will output an empty string . If value It's a None value , The above code will output nothing.
Returns a list of / Tuples / The first element in the string . The sample code is as follows :
{{ value|first }} If value Is equal to ['a','b','c'], So the output will be a.
Returns a list of / Tuples / The last element in the string . The sample code is as follows :
{{ value|last }} If value Is equal to ['a','b','c'], So the output will be c.
Use rounding to format a floating point type . If the filter does not pass any parameters . Then only one decimal will be kept after the decimal point , If all the decimals are followed by 0, Then only integers... Will be preserved . Of course, you can also pass a parameter , How many decimals should be reserved for identification .
{{ value\|floatformat }} | 34.2 | | 34.000 | {{ value\|floatformat }} | 34 | | 34.260 | {{ value\|floatformat }} | 34.3 |{{value\|floatformat:3}} | 34.232 | | 34.0000 | {{value\|floatformat:3}} | 34.000 | | 34.26000 | {{value\|floatformat:3}} | 34.260 | Similar to Python Medium join, Will list / Tuples / Strings are spliced with the specified characters . The sample code is as follows :
{{ value|join:"/" }} If value Is equal to ['a','b','c'], Then the above code will output a/b/c.
Get a list / Tuples / character string / The length of the dictionary . The sample code is as follows :
{{ value|length }} If value Is equal to ['a','b','c'], Then the above code will output 3. If value by None, So the above will return to 0.
All characters in the value are converted to lowercase . The sample code is as follows :
{{ value|lower }} If value Is equal to Hello World. Then the above code will output hello world.
Be similar to lower, Just convert all the specified strings to uppercase .
In the list given / character string / Select a value randomly in the tuple . The sample code is as follows :
{{ value|random }} If value Is equal to ['a','b','c'], Then the above code will randomly select one in the list .
Marking a string is safe . That is to say, it will turn off the automatic escape of this string . The sample code is as follows :
{{value|safe}} If value Is a string that does not contain any special characters , such as <a> such , Then the above code will input the string normally . If value It's a bunch of html Code , So the above code will put this html Code rendering to browser .
Be similar to Python Slice operations in . The sample code is as follows :
{{ some_list|slice:"2:" }} The above code will give some_list from 2 Start slicing .
Delete all... In the string html label . The sample code is as follows :
{{ value|striptags }} If value yes <strong>hello world</strong>, Then the above code will output hello world.
If the given string length exceeds the length specified by the filter . So it's going to cut , And will splice three dots as ellipsis . The sample code is as follows :
{{ value|truncatechars:5 }} If value Is equal to Welcome to Beijing ~, So the output is Beijing .... Maybe you think , Why not Welcome to Beijing ... Well . Because three dots take up three characters , therefore Beijing + The character length of three dots is 5.
Be similar to truncatechars, Just can't cut html label . The sample code is as follows :
{{ value|truncatechars:5 }} If value Is equal to <p> Welcome to Beijing ~</p>, So the output will be <p> Beijing ...</p>.
Official recommendation: there are six ways for pandas to read excel, and the correct answers are written in the source code ~ its too convenient
Hello everyone , This is Wang