Moment For Technology

Custom filters implement the built-in lower

Posted on Aug. 8, 2022, 7:07 p.m. by Jared Finley
Category: The back-end Tag: The back-end django

Introduction -- Analyzing Djangos built-in template filters:

The built-in filter can be interpreted as: a Python function with one or two arguments:

  1. The value of an input variable.
  2. Parameter value - Can have an initial value, or no parameter at all.

Custom filter

  1. Create a new app named ceshi for use in this article.
  2. Customize the location of the filter and tags in the TemplateTags directory, which we chose to use for the new app (which means the app must be registered!). ;

The first -- implement the built-in filter lower! (No parameter)

Templatetags templateTags create a file called py, and write a custom filter. You can name the file any way you like. Here I use common_extras.py:

(2) Use of built-in filter lower:

  1. Route assignment in urls.py in the project directory;
  2. Create a template file: templates/ceshi/test1. HTML;
  3. Set the route in the new app and write the view function

View function: (ceshi/views.py file)

from django.shortcuts import render

# Create your views here.


def test1(request) :
    str1 = "Cool Boy"
    return render(request, "ceshi/test1.html", context={"str1": str1})
Copy the code

Deform the template using a filter by passing a string str1 to the template.

  1. Template file authoring (using built-in filters) :

(templates/ceshi/test1. HTML file)

! DOCTYPEhtml
html lang="en"
head
    meta charset="UTF-8"
    titleTitle/title
/head
bodyString: {{str1}}brThe use of built-in filters: {{str1 | lower}}/body
/html
Copy the code
  1. Lower filter function: change all variables to lowercase

(3) Use of custom filters:

(Common_extras.py file)

Note: custom filters must be decorated with Library().filter before they can be used as filters!

# for writing custom filters and tags
from django import template

register = template.Library()

@register.filter				Add this decorator to register the custom filter. It's ready to use!
def mylower(value) :      # value Receive variable
    return value.lower()
Copy the code

Explanation:

django.template.Library.filter()

  1. The library.filter () method takes two arguments: a. The name of the filter(a string object) b. Compiled function - a Python function (do not write function names as strings)
  2. You can use register.filter() as a decorator (common method, and used in this article!). ;
  3. If you declare the name parameter, Django uses the value of the nam parameter as the filter name. For example, @register.filter(name="lowermy"). If you want to use the custom filter, you need to use the lowermy name.
  4. Without declaring the name parameter, Django uses the function name as the filter name.

Here are some less common methods:

def mycut(value,arg):
    return value.replace(arg,"")
register.filter("mycut",mycut)
Copy the code

(4) Use custom filters in the template:

(templates/ceshi/test1. HTML file)

Note: {%load name%} must be used in the template to import the py file where the custom filter function resides. Name is the name of the py file. Custom filters are used in the same way as built-in filters!

(5) Effect display:

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.