Get the App
SLTechnology News&Howtos  ›  Network Security  › 

What is the middleware of Django?

Shulou Source: shulou.com Published: 2022-06-01 00:42:00 09月23日 Update

This article mainly explains "what is the middleware of Django". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the middleware of Django?"

What is middleware (middleware)

Middleware (middleware) in django, in django, middleware is actually a class. After the request arrives and ends, django will execute the corresponding methods in the middleware at the right time according to its own rules.

In the settings module of the django project, there is a MIDDLEWARE_CLASSES variable, and each element is a middleware

Custom middleware:

1. One or more methods that can be defined in middleware:

Process_request (self,request) send request

Process_view (self,request, view_func, view_args, view_kwargs) views function processing

Process_template_response (self,request, response)

Process_exception (self,request, exception) exception handling is performed only when views reports an error

Process_response (self,request, response) returns the result

2. Create a middleware class a, create a new XXX package or folder b, create a new AAA.py module c, write as the following class in AAA.py, the class contains one or more of the following functions, function name and parameters cannot be changed d, import middleware module, from django.utils.deprecation import MiddlewareMixin

MIDDLEWARE = [

'common.middleware.auth.AuthMiddleware'

]

At this point, I believe you have a deeper understanding of "what is the middleware of Django". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Middleware functions methods modules content multiple that is processing learning appropriate practical deeper elements interests parameters variables names practicality practicality simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Xiaomi macOS NVidia