1. 程式人生 > >Django 中間件

Django 中間件

size style 繼承 cli all 處理 messages s函數 tro

Django默認的Middleware有七個

MIDDLEWARE = [
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware
, django.contrib.messages.middleware.MessageMiddleware, django.middleware.clickjacking.XFrameOptionsMiddleware, ]

流程

process_request -->返回第一個 -->process_view-->view函數-->process_exception-->返回最後一個-->process_response

技術分享圖片

自定義中間件

中間件最多有五個方法

process_request

process_view

process_exception

process_response

procrss_template——response

process_request,process_response

當用戶發起請求的時候會依次經過所有的的中間件,這個時候的請求時process_request,最後到達views的函數中,views函數處理後,在依次穿過中間件,這個時候是process_response,最後返回給請求者。

技術分享圖片

上述截圖中的中間件都是django中的,我們也可以自己定義一個中間件,我們可以自己寫一個類,但是必須繼承MiddlewareMixin

需要導入

from django.utils.deprecation import MiddlewareMixin

技術分享圖片

in views:

def index(request):

    
print("view函數...") return HttpResponse("OK")

in Mymiddlewares.py:

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse

class Md1(MiddlewareMixin):

    def process_request(self,request):
        print("Md1請求")
 
    def process_response(self,request,response):
        print("Md1返回")
        return response

class Md2(MiddlewareMixin):

    def process_request(self,request):
        print("Md2請求")
        #return HttpResponse("Md2中斷")
    def process_response(self,request,response):
        print("Md2返回")
        return response

結果:

Md1請求
Md2請求
view函數...
Md2返回
Md1返回

註意:如果當請求到達請求2的時候直接不符合條件返回,即return HttpResponse("Md2中斷"),程序將把請求直接發給中間件2返回,然後依次返回到請求者,結果如下:

Md1請求
Md2請求
Md2返回
Md1返回

process_view

process_view(self, request, callback, callback_args, callback_kwargs)

Mymiddlewares.py修改如下

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse

class Md1(MiddlewareMixin):

    def process_request(self,request):
        print("Md1請求")
        #return HttpResponse("Md1中斷")
    def process_response(self,request,response):
        print("Md1返回")
        return response

    def process_view(self, request, callback, callback_args, callback_kwargs):
        print("Md1view")

class Md2(MiddlewareMixin):

    def process_request(self,request):
        print("Md2請求")
        return HttpResponse("Md2中斷")
    def process_response(self,request,response):
        print("Md2返回")
        return response

    def process_view(self, request, callback, callback_args, callback_kwargs):
        print("Md2view")

結果如下:

Md1請求
Md2請求
Md1view
Md2view
view函數...
Md2返回
Md1返回

技術分享圖片

最後一個中間的process_request到達路由關系映射之後,返回到中間件1的process_view,然後依次往下,到達views函數,最後通過process_response依次返回到達用戶。

process_view可以用來調用視圖函數:

class Md1(MiddlewareMixin):

    def process_request(self,request):
        print("Md1請求")
        #return HttpResponse("Md1中斷")
    def process_response(self,request,response):
        print("Md1返回")
        return response

    def process_view(self, request, callback, callback_args, callback_kwargs):

        # return HttpResponse("hello")

        response=callback(request,*callback_args,**callback_kwargs)
        return response

結果如下:

Md1請求
Md2請求
view函數...
Md2返回
Md1返回

註意:process_view如果有返回值,會越過其他的process_view以及視圖函數,但是所有的process_response都還會執行。

process_exception

	
process_exception(self, request, exception)

示例修改如下:

技術分享圖片
class Md1(MiddlewareMixin):

    def process_request(self,request):
        print("Md1請求")
        #return HttpResponse("Md1中斷")
    def process_response(self,request,response):
        print("Md1返回")
        return response

    def process_view(self, request, callback, callback_args, callback_kwargs):

        # return HttpResponse("hello")

        # response=callback(request,*callback_args,**callback_kwargs)
        # return response
        print("md1 process_view...")

    def process_exception(self):
        print("md1 process_exception...")



class Md2(MiddlewareMixin):

    def process_request(self,request):
        print("Md2請求")
        # return HttpResponse("Md2中斷")
    def process_response(self,request,response):
        print("Md2返回")
        return response
    def process_view(self, request, callback, callback_args, callback_kwargs):
        print("md2 process_view...")

    def process_exception(self):
        print("md1 process_exception...")
View Code

結果如下:

技術分享圖片
Md1請求
Md2請求
md1 process_view...
md2 process_view...
view函數...

Md2返回
Md1返回
View Code

流程圖如下:

當views出現錯誤時:

技術分享圖片

md2的process_exception修改如下:

 def process_exception(self,request,exception):

        print("md2 process_exception...")
        return HttpResponse("error")

結果如下:

Md1請求
Md2請求
md1 process_view...
md2 process_view...
view函數...
md2 process_exception...
Md2返回
Md1返回

中間件之process_template_response(request, response)

process_template_response(self,request,response)

只有當views函數中返回的對象中具有render方法,是就會直接process_template_responseprocess

Django 中間件