Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction: We covered the use of template variables in Django earlier. It’s easy to use, but what about template variables? The answer is yes, and that’s what this article is about — filters!

1. The filter

(1) Explanation of pure dry goods:

Function: Filter variables. Before rendering, the filter will process the variables according to the function, and then replace the original variables to display the results.
  • Grammar: {{fruits | lower}}

  • The pipe symbol makes a chain call (can be understood as nested use!) For example, implementing a feature that converts all characters to lowercase and the first character to uppercase.
  • Grammar: {{fruits | lower | capfirst}}

  • ** Use arguments: Filters can use arguments, followed by colons (), double quotes (), and arguments (). For example, to remove all whitespace from a string, use the cut filter.

Grammar: {{fruits | the cut: “”}} * *

  • Note:

When using arguments, there must be no space between the colon and the argument.

(2) Django filters:

The views.py file in the project directory:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

from datetime import datetime
def index(request) :
    test="THIS IS A LIST!"
    list= ["A"."B"."C"."D"."E"]
    data={
        'test':test,
        'xx':' '.'num1':1.'num2':2.'list':list.'now':datetime.now(),
        'html':'

hello django!!!

'
.'float':3.1415926 } return render(request, "index.html", data) Copy the code

The index.html file in the templates folder of the project directory

<! DOCTYPE html> <html lang="en">
<head>
    <meta charset="UTF-8"> <title> Test! </title> </head> <body> {Unprocessed variables: #}
{{ test }}<br>

{# variables with a default value will use this value if Django considers it False. For example, an empty string,None. #}
{{ xx|default:'xxxxx' }}<br>

{# set the variable to use the default value only if it is None #}
{{ xx|default_if_none:'aaaaa'}}<br>

{Variable #}
{{ test|lower }}<br>

{Variable #}
{{ test|lower|capfirst }}<br>

{Add: string add, number add, list add, if failed, will return an empty string #}
{{ num1|add:num2 }}<br>

{Add two string variables #}
{{ test|add:xx }}<br>

{The first element of the list variable #}
{{ list|first }}<br>

{The last element of the list variable #}
{{ list|last }}<br>

{Default date-time format for data #}
{{ now|date }}<br>
{# default time format #}
{{ now|time }}<br>
{This is the 24-hour time format of the data filter #}
{{ now|date:'Y/m/d/H:i:s' }}<br>
{# this is the time filter in the 12-hour time format #}
{{ now|time:'h:i:s' }}<br>

{This is the same as joining in Python #}
{{ list|join:'oooo' }}<br>

{This is the string length method #}
{{ test|length }}<br>

{This is whether the length of the list is 4#}
{{ list|length_is:4 }}<br>

{* * * * * * * * * * * * * * * * *
{# this is a string that displays only 4 characters, leaving out the rest (but taking up 3 bits) #}
{{ test|truncatechars:7 }}<br>
{# this is a string that displays only 2 words, leaving out the rest (no placeholders) #}
{{ test|truncatewords:2 }}<br>

{This is a slice of string #}
{{ test|slice:'1:4' }}<br>

{This is a slice of the list #}
{{ list|slice:'2' }}<br>

{This is a string with an HTML tag #}
{{ html }}<br>

{This is to remove the HTML tag #} from the string.
{{ html|striptags }}<br>

{This is to turn off auto-escape and enable HTML tags in strings #}
{{ html|safe }}<br>

{# This is an unprocessed decimal #}
{{ float }}<br>
{This is a decimal number.
{{ float|floatformat }}<br>
{This is two decimal places.
{{ float|floatformat:'2' }}

</body>
</html>
Copy the code

③ Effect display:

Date and time filter formats:

Extension – Introduction to automatic escape:

What is automatic escape?

Automatic escape is to escape some special characters of a variable, such as the left arrow (<) and right arrow (>), into HTML code in order to handle some unsafe variables.

< : escape into &lt; > < span style = "max-width: 100%; ': escape into &# 39;": escape into"; & : escape to &amp;Copy the code

🔆 In The End!

Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!

If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!