程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pychart developing Django project template common filter tutorial

編輯:Python

Template commonly used filter

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

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 ''

cut

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

date

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

default

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.

default_if_none

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.

first

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.

last

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.

floatformat

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 .

  1. If no parameters are passed : | value | Template code | Output | | --- | --- | --- | | 34.23234 | {{ value\|floatformat }} | 34.2 | | 34.000 | {{ value\|floatformat }} | 34 | | 34.260 | {{ value\|floatformat }} | 34.3 |
  2. If you pass parameters : | value | Template code | Output | | --- | --- | --- | | 34.23234 | {{value\|floatformat:3}} | 34.232 | | 34.0000 | {{value\|floatformat:3}} | 34.000 | | 34.26000 | {{value\|floatformat:3}} | 34.260 |

join

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.

length

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.

lower

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.

upper

Be similar to lower, Just convert all the specified strings to uppercase .

random

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 .

safe

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 .

slice

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 .

stringtags

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.

truncatechars

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.

truncatechars_html

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>.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved