Detailed Introduction to Django Middleware

Source: Internet
Author: User

Writing a few weeks of script, today finally began to touch the web framework ~ Learning Python web framework, then Django is almost a compulsory course, this time the job is to hit first, the main task is to add a version number in setting, in the rendering of static Css,js path with the version number, For example, "Example.v1124.css", and then remove the version number from the request. Why did you do it? Because of this, the path to the front-end output static file will be added with the version number, so when the development of a new static file, the client will force to flush the local cache, in order to achieve this goal must first be configured in the settings file, This allows you to read the version number directly from settings each time you modify it.

One, the request period of Django middleware

We send a request from the browser to get a response to the content HttpResponse, the process of passing the request to Django is as follows:

That is, each request is first through the Process_request function in the middleware, this function returns None or HttpResponse object, if return to the former, continue processing other middleware, if return a httpresponse, processing abort, Return to the Web page.

Ii. Customization and Registration middleware

1. Creating middleware

To create a middleware py file under Project root:

 1 class Requestexeute (object): 2 def process_request (self, request): 3 print (' Process_request ') 4 5 def Process_view (self, request, callback, Callback_args, Callback_kwargs): 6 print (' Process_view ') 7 8 def Proces S_exception (self, request, exception): 9 "" "10 when the views function fails:p Aram Request:12:p Aram E Xception:13:return:14 "" "Print (' process_exception ') + def process_response (SE LF, request, response): 18 "" 19 must return HTTPRESPONSE20:p Aram request:21:p Aram Response : 22:return:23 "" "Print (' Process_response ') return Response26 def process_te         Mplate_response (self, request, response): 28 "" "in the return value of the 29 view function, it is called if there is a render method:p Aram Request:31 :p Aram response:32:return:33 "" "Print (' Process_template_response ') return res Ponse
md.py

2. Register Middleware

In the settings.py file, register in the order in which you want to execute:

PS. In the 1.9 and earlier versions, the Middleware keyword is: middleware_classes

middleware_classes = (' Zqxt.middleware.BlockedIpMiddleware ',    ... Other middleware)

Third, the execution order of the methods in the middleware

1. Normal condition

 1 from django.shortcuts import HttpResponse 2 from django.utils.deprecation import Middlewaremixin 3 class M1 (Middlewarem Ixin): 4 def process_request (self, request): 5 print (' M1.request ') 6 # return HttpResponse (' request_m1         ') 7 8 def process_view (self, request, callback, Callback_args, Callback_kwargs): 9 print (' M1.view ') 10 # response = callback (Request, *callback_args, **callback_kwargs) # return Response12 def PROCESS_RESPO NSE (self, request, response): print (' M1.response ') return RESPONSE16-def process_exception (self     , request, Exception): Print (' m1.process_exception ') return HttpResponse (' m1.process_exception ') 20 21 def process_template_response (self, request, response): 22 "" "in the return value of the 23 view function, 24 is called if there is a render method: Param request:25:p Aram response:26:return:27 "" "Print (' M1.process_template_response ') Return responSe30 class M2 (middlewaremixin): Def process_request (self, request): Print (' m2.request ') + def Process_view (self, request, callback, Callback_args, Callback_kwargs): Notoginseng print (' M2.view ') # response = Callback (Request, *callback_args, **callback_kwargs) # return RESPONSE40 + def process_response (self, reque St, Response): print (' M2.response ') return response44 def process_exception (self, request, exce ption): m2.process_exception (' m2.process_exception ')-HttpResponse def process _template_response (self, request, response): 50 "" "in the return value of the 51 view function, if there is a render method, it is called:p Aram Request:5 3:p Aram response:54:return:55 "" "" "," "" "" "" "" "" "" M2.process_template_response " RN Response
Process_request and Process_view return none

Execution order:

    1. M1.request

    2. M2.request

    3. M1.view

    4. M2.view

    5. M2.response

    6. M1.response

2. In version 1.10 and after

If Process_request returns the HttpResponse object, it returns from the process_response of the current middleware

 1 from django.shortcuts import HttpResponse 2 from django.utils.deprecation import Middlewaremixin 3 class M1 (Middlewarem  Ixin): 4 def process_request (self, request): 5 print (' M1.request ') 6 return HttpResponse (' request_m1 ')         7 8 def process_view (self, request, callback, Callback_args, Callback_kwargs): 9 print (' M1.view ') 10 # response = callback (Request, *callback_args, **callback_kwargs) # return Response12 def process_respons E (Self, request, response): print (' M1.response ') return RESPONSE16-def process_exception (self,     Request, Exception): Print (' m1.process_exception ') return HttpResponse (' m1.process_exception ') 20 21 def process_template_response (self, request, response): 22 "" "The return value of the 23 view function is called if there is a render method:p a          Ram request:25:p Aram response:26:return:27 "" "Print (' M1.process_template_response ') 29 return responseClass M2 (middlewaremixin): Def process_request (self, request): Print (' m2.request ') + def p Rocess_view (self, request, callback, Callback_args, Callback_kwargs): Notoginseng print (' M2.view ') # response = CA Llback (Request, *callback_args, **callback_kwargs) # return RESPONSE40 + def process_response (self, request , response): print (' M2.response ') return response44 def process_exception (self, request, except ION): HttpResponse (' m2.process_exception ')-M2.process_exception def process_t         Emplate_response (self, request, response): 50 "" "in the return value of the 51 view function, if there is a render method, it is called:p Aram request:53 RESPONSE58 # class Requestexeute (object): # def process_request (self, request): # print (' Process_re Quest ') #64 # defProcess_view (self, request, callback, Callback_args, Callback_kwargs): # print (' Process_view ') #67 # def P         Rocess_exception (self, request, exception): 68 # "" "69 # when the Views function fails, execute the #:p Aram Request:71 #         Response (Self, request, response): 77 # "" "78 # must return HttpResponse79 #:p Aram Request:80 # #86 # def process_template_response (self, request, response): 87 # "" "88 # The return value of the View function is adjusted if there is a Render method With the Aram #:p request:90 #:p Aram response:91 #: return:92 # "" _template_response ') 94 # return response
M1 's process_request returns HttpResponse

Execution order:

    1. M1.request

    2. M1.response

Process_view is similar to process_request:

 1 from django.shortcuts import HttpResponse 2 from django.utils.deprecation import Middlewaremixin 3 class M1 (middleware Mixin): 4 def process_request (self, request): 5 print (' M1.request ') 6 # return HttpResponse (' Reques         T_m1 ') 7 8 def process_view (self, request, callback, Callback_args, Callback_kwargs): 9 print (' M1.view ') 10 Response = callback (Request, *callback_args, **callback_kwargs) return response12 def process_re Sponse (self, request, response): print (' M1.response ') return RESPONSE16 + def process_exception ( Self, request, exception): Print (' m1.process_exception ') return HttpResponse (' m1.process_exception ')         def process_template_response (self, request, response): 22 "" "23 the return value of the view function is called if there is a Render method 24 :p Aram request:25:p Aram response:26:return:27 "" "Print (' M1.process_template_resp Onse ') returnResponse30 class M2 (middlewaremixin): Def process_request (self, request): Print (' m2.request ') 35 36 def process_view (self, request, callback, Callback_args, Callback_kwargs): Notoginseng print (' M2.view ') # RESPO  NSE = callback (Request, *callback_args, **callback_kwargs) # return RESPONSE40 + def process_response (self, Request, Response): print (' M2.response ') return response44 def process_exception (self, request , exception): P-Print (' m2.process_exception ')-Return HttpResponse (' m2.process_exception ') Rocess_template_response (self, request, response): 50 "" "in the return value of the 51 view function, if there is a render method, it is called:p Aram req         uest:53:p Aram response:54:return:55 "" "" "" "" "" "" "" "" 57 return response
M1 's Process_view returns HttpResponse

Execution order:

    1. M1.requestm2.requestm1.viewm2.responsem1.respons

3. Prior to version 1.10

If Process_request returns the HttpResponse object, the process_request after the current middleware is not executed, and the Process_response forward from the last middleware is returned

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.