A deep understanding of Django's custom filters and a deep understanding of django

Source: Internet
Author: User

A deep understanding of Django's custom filters and a deep understanding of django

Preface

This article mainly introduces you to the Django custom filter and shares it for your reference. I will not talk much about it below. Let's take a look at the details:

Filters and functions

Django filters are essentially functions, but there are too many "functions". To show their uniqueness, designers think of a name "filter "...

Django has some built-in filters, but there are not many "Newbie racing cars" (convert the letters into lowercase, evaluate the array length, and take a random value from the array ), basic functions (not powerful enough )...

With a mentality of "researching and pondering", I tried to write a filter by myself: The function is very simple. I want to find the maximum value in an array (it's too late today, so I am so lazy ~)

The function is simple:


Filter

First, let me have some experience:

In django's MVP architecture, the data Model is handed over to the Model, the logic processing is handed over to the View, and the style temPlate is handed over to the temPlate, which is recognized as a reasonable architecture.

As the project grows, some problems may occur. view manages all logical processing and some basic logical functions (such as finding the maximum and minimum values, and adding a prefix to the user name) there is no need to put it together with the core logic, which not only makes the code redundant, but also affects the iterative maintenance of the core module...

Therefore, the django designer decided to extract some basic and general logic modules and put them in the templatetags folder under the app directory (the folder name cannot be changed; otherwise, django will not be able to recognize them ), templatetags can store multiple files, and each file can store multiple function-related filters. When using function-related filters, you can directly import a single file and use related functions together. It is worthy of being django, the framework design is really awesome!

Specific rules:

1. If a filter is used for a webpage under an app, create a folder named templatetags under the root directory of the corresponding app (the pad here is an independent app)


Create directory

2. in the templatetags folder, create an empty file _ init __. py (this is a python rule, __init __. py indicates that its folder is a module that can be loaded, __init _ the file content can be blank). Create the file my_filter.py (this file can store multiple filters, the file name can be customized ...)


Create a file

3. In my_filter.py, import the header file and write the filter function (this is a lazy one. Use python's built-in function for demonstration), and register the filter (or like the decorator );

From django. template import Library # instantiate the registration class as the register object register = Library () # register @ register with the decorator. filterdef get_list_max (val): # val is a list, and the maximum value result = max (val) return result is obtained through the filter.

4. Load the filter to html and use the custom filter.

<! DOCTYPE html> 

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.