django--Middleware

Source: Internet
Author: User

1. Principle

In Django, the middleware is actually a class, and after the request arrives and ends, Django executes the appropriate method in the middleware at the right time according to its own rules.

When the request arrives, there is a layer of middleware in the middle of the WSGI and URLs, exactly the 4-tier approach and the Django Framework's handling:

    1. Process_request (Self,request)

    2. Process_view (self, request, callback, Callback_args, Callback_kwargs)

    3. Views

    4. process_exception (Self, request, exception)

    5. Process_response (self, request, response)

The names and parameters of these functions cannot be changed, and any link with response will be handed to Process_response () to be processed and returned to the user.


2. Custom MiddlewareCreate a Package


middle1.py
1234567891011121314151617 class mmm(object):    def process_request(self,request):        print "mmm.process_request"    def process_view(self,request,callback,callback_args,callback_kwargs):        print "mmm.process_view"    def process_response(self,request,response):        print "mmm.process_response"        return responseclass xxx(object):    def process_request(self,request):        print "xxx.process_request"    def process_view(self,request,callback,callback_args,callback_kwargs):        print "xxx.process_view"    def process_response(self,request,response):        print "xxx.process_response"        returnresponse
settings.py

123456789101112 MIDDLEWARE_CLASSES = [    ‘django.middleware.security.SecurityMiddleware‘,    ‘django.contrib.sessions.middleware.SessionMiddleware‘,    ‘django.middleware.common.CommonMiddleware‘,    # ‘django.middleware.csrf.CsrfViewMiddleware‘,    ‘django.contrib.auth.middleware.AuthenticationMiddleware‘,    ‘django.contrib.auth.middleware.SessionAuthenticationMiddleware‘,    ‘django.contrib.messages.middleware.MessageMiddleware‘,    ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,    ‘middleware.middle1.mmm‘,    ‘middleware.middle1.xxx‘,]
views.py
123 defindex(request):    print "view"    returnHttpResponse("index")
Ide


You can see the order of execution, noting that the order of execution of Process_response () is from backward forward.


3. Source code
123456789101112131415161718192021222324 #Django自动去settings里找 MIDDLEWARE_CLASSES process_request_list=[]process_view_list=[]process_response_list=[]process_exception_list=[]for class inMIDDLEWARE_CLASSES:    obj = class()    if hasattr(obj,process_request):        process_request_list.append(obj.process_request)    if hasattr(obj,process_view):        process_view_list.append(obj.process_view)    if hasattr(obj,process_response):        process_response_list.append(obj.process_response)    if hasattr(obj,process_exception):        process_exception_list.append(obj.process_exception)for i in process_request_list:    i()for i in process_view_list:    i()#Django渲染for i in process_exception_list:    i()for i in process_response_list:    i()


4.process_exceptionviews.py
12 defindex(request):    raiseException("报错了")
middleware/middle.py
1234567891011 class mmm(object):    def process_request(self,request):        print "mmm.process_request"    def process_view(self,request,callback,callback_args,callback_kwargs):        print "mmm.process_view"    def process_response(self,request,response):        print "mmm.process_response"        returnresponse    def process_exception(self, request, exception):        print exception        return request
Browser


IDE


Attention:?
    • The Process_request and Process_view methods are executed in the order of the custom middleware

    • Process_exception and Process_response are executed in the order of the middleware from the back forward













From for notes (Wiz)

django--Middleware

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.