Django template system — filters
Filter, change of display form of variables
1. Form: lowercase
{{ name | lower }}
2. Concatenation: first escape the text to HTML, and then convert each line to
tag
{{ my_text|escape|linebreaks }}
3. Filter parameters
Display the first 30 words
{{ bio | truncatewords: "30" }}
Formatting
{{ pub_date | date: "F j, Y" }}
Filter list
{{ 123|add: "5" }} Add a value to value
{{ "AB'CD"|addslashes }} Single quotes plus escape marks are generally used for output to javascript
{{ "abcd"|capfirst }} The first letter is capitalized
{{ "abcd"|center:"50" }} Output specification The length of the string and center the value
{{ "123spam456spam789"|cut:"spam" }} Find and delete the specified string
{{ value|date:"F j, Y" }} Format the date
{{ value|default:"(N/A)" }} If the value does not exist, use the specified value
{{ value|default_if_none:"(N/A)" }} If the value is None, use the specified value
{ {List variable|dictsort:"Number"}} Sort from small to large
{{ List variable|dictsortreversed:"Number" }} Sort from large to small
{% if 92|pisibleby:"2" %} Determine whether Divides the specified number
{{ string|escape }} and converts it to html entity
{{ 21984124|filesizeformat }} Using 1024 as the base, calculates the maximum value, retaining 1 decimal place, increasing readability
{{ list|first }} Return the first element of the list
{{ "ik23hr&jqwh"|fix_ampersands }} & Convert to &
{{ 13.414121241|floatformat }} Retain 1 decimal place, can be a negative number, several forms
{{ 13.414121241| floatformat: "2" }} Keep 2 decimal places
{{ 23456 |get_digit: "1" }} Intercept a number at the specified position starting from the single digit
{{ list|join: ", " }} Use Specify the delimiter to connect the list
{{ list|length }} Return the number of lists
{% if list|length_is: "3" %} Whether the number of lists specifies a value
{{ "ABCD"|linebreaks }} Use New lines are marked with
,
wrapped
{{ "ABCD"|linebreaksbr }} New lines are marked with
wrapped
{{ variable|linenumbers }} Add line number to each line in the variable
{{ "abcd "|ljust:"50" }} Align the string to the left in the specified width, fill the rest with spaces
{{ "ABCD"|lower }} lowercase
{% for i in "1abc1"|make_list %}ABCDE ,{% endfor %} Use the number of characters in a string or number as a list
{{ "abcdefghijklmnopqrstuvwxyz"|phone2numeric }} Convert characters into corresponding numbers? ?
{{ list or number | pluralize }} The plural form of the word, if the number of strings in the list is greater than 1, return s, otherwise return an empty string
{{ list or number | pluralize: "es" }} Specify es
{{ list or number|pluralize:"y,ies" }} Specifies that ies is replaced by y
{{ object|pprint }} displays the value of an object
{{ list|random }} returns a random item in the list
{{ string|removetags:"br p p" }} Delete the specified html tag in the string
{{ string|rjust: "50" }} Align the string to the right in the specified width, and fill the rest with spaces
{{ list|slice:":2" }} Slice
{{ string|slugify }} The minus sign and underscore are left in the string, other symbols are deleted, and spaces are replaced with minus signs
{{ 3|stringformat:" 02i" }} String format, using Python's string format syntax
{{ "EABD"|striptags }} to strip [X]HTML syntax tags
{{ time variable|time:"P" }} date Time part format
{{ datetime|timesince }} How much time has passed since the given date until now
{{ datetime|timesince:"other_datetime" }} How much time has passed between two days
{{ datetime|timeuntil }} How much time has elapsed since a given date? The difference from the above is the position before and after the 2 dates.
{{ datetime|timeuntil:"other_datetime" }} How much time has passed between two days
{{ "abdsadf"|title }} capitalized
{{ "A B C D E F" | truncatewords: "3" }} intercepted Specify the number of words
{{ "111221"|truncatewords_html:"2" }} Intercept the specified number of html tags and complete them
{{ list|unordered_list }}
Multiple embeddings The set list is displayed as an unordered list of html
{{ string|upper }} All caps
linkage url encoding
{{ string|urlize }} Convert URLs from plain text to clickable links. (No experiment was successful)
{{ string|urlizetrunc: "30" }} Same as above, multiple intercepted character numbers. (Similarly no experiment was successful)
{{ "B C D E F"|wordcount }} Number of words
{{ "a b c d e f g h i j k"|wordwrap: "5" }} Insert carriage return characters after every specified number of characters
{{ boolean| yesno:"Yes,No,Perhaps" }} The return string for three values, corresponding to non-empty, empty, None

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...

Fastapi ...
