Django Learning: Middleware

First, the middleware

1. What is middleware?

Middleware name implies, is interposed between a processing request and response process, relatively lightweight, and changes in the input and output django globally. Because change is global, it is necessary to be cautious and practical, with good will affect the performance.

2, what did?
  User login
  logging
  crsf: for all post requests to do a verification
  session
  Rights Management

3、

Note:
  do batch processing of all requests for time middleware used
  alone do certain processing functions when used decorator

4, using the steps of:

Copy the code
Step: 
1 ,, to build a folder, which is written a py file 2, and then start writing class 1. middleware is a Class A, there are several ways to write class M1 (MiddlewareMixin): must inherit def process_request (self, request): request: request everything inside of print ( "m1.request_request") this method which do not easily return value, return value if there is no longer continue to back up, and perform their own process_response top of the response is generally no return value: continue to perform subsequent middleware and view functions DEF process_response (Self, Request, the Response): return the Response 2. mIDDLEWARE in settings plus the path to the folder name .py file name of the class name. 3. locate inheritance of that class, it took over the class in general do not use the method of introduction, or sometimes there is no update of this class, you put it inherits that class take over,
Copy the code

Graphical analysis process:

process_reques return value:

 

process_reques None Return Value:

 

In the middleware settings:

 

 

Example:

Copy the code
class MiddlewareMixin(object):
    def __init__(self, get_response=None):
        self.get_response = get_response
        super(MiddlewareMixin, self).__init__()

    def __call__(self, request):
        response = None
        if hasattr(self, 'process_request'):
            response = self.process_request(request)
        if not response:
            response = self.get_response(request)
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response)
        return response

# 至少要有两个类
class Md1(MiddlewareMixin):  #必须继承
    def process_request(self,request):
        print("md1===process_request")
        l = ["/login/"]
        # request.path_info: The current path 
        if request.path_info in l: # Because login is not validated, directly or none on the line 
            return None 
        IF not request.session.get (settings.GDP): 
            return redirect ( "/ login / ") 
        # 
        # If no return value, and continue to follow the middleware view function 
        # If there is a return value, on the implementation of their process_response and above the Response 
    DEF process_response (Self, Request, the Response): 
        Print (" MD1 === process_response1 = ") 
        return the Response # has to return the 

class Md2 (MiddlewareMixin): 
    def process_request(self,request):
        print("md2====process_request2")
    def process_response(self,request,response):
        Print (" MD2 ==== process_response2 ") 
        return the Response
    
    
    
    
Copy the code

test:

def testMD(request):
    print("view.test")
    return HttpResponse("...") 

Return result:

Guess you like

Origin www.cnblogs.com/lida585/p/10990819.html