Django中的MiddleWare中間件

1. middleware簡介django


 

Django的middleware的概念至關於SSH框架裏面的filter的概念。中間鍵的做用就是對全部的request,在request前,和在response後作必定的處理。session

Django的中間鍵類型分爲五種:框架

   請求(Request)中間件->對應函數process_request
  視圖(View)中間件->對應函數process_view
  模板(Template)中間件->對應函數process_template_response(不經常使用)
  響應(Response)中間件->對應函數process_response
  異常(Exception)中間件->對應函數process_exception(不經常使用)函數

2. middleware中間鍵函數的執行順序和過程spa


 (1)請求(Request)中間件和響應(Response)中間件的正常執行順序3d

接下來就用程序演示:code

m1.pycsrf

from django.utils.deprecation import MiddlewareMixin

class ROW1(MiddlewareMixin):
    def process_request(self,request):
        print("朱三")
def process_response(self,request,response): print('趙六') return response class ROW2(MiddlewareMixin): def process_request(self,request): print("李四") def process_response(self,request,response): print('薛七') return response class ROW3(MiddlewareMixin): def process_request(self,request): print("王五")
def process_response(self,request,response): print('宋八') return response

views.py中間件

def test(request):
    # int("asdsadsad")
    print('小豬->沒帶錢')
    return HttpResponse("OK")

因此順序爲:朱三-->李四-->王五-->小豬-->沒帶錢-->宋八-->薛七-->趙六blog

若是在中間件中發生錯誤,那麼其執行順序爲

 m1.py

from django.utils.deprecation import MiddlewareMixin

class ROW1(MiddlewareMixin):
    def process_request(self,request):
        print("朱三")
    def process_response(self,request,response):
        print('趙六')
        return response
    

from django.shortcuts import HttpResponse
class ROW2(MiddlewareMixin):
    def process_request(self,request):
        print("李四")
        return HttpResponse('離開')

    def process_response(self,request,response):
        print('薛七')
        return response


class ROW3(MiddlewareMixin):
    def process_request(self,request):
        print("王五")

    def process_response(self,request,response):
        print('宋八')
        return response
    

views與上面同樣,執行順序爲:朱三-->李四-->薛七-->趙六

 (2)視圖中間件執行順序

from django.utils.deprecation import MiddlewareMixin

class ROW1(MiddlewareMixin):
    def process_request(self,request):
        print("朱三")

    def process_response(self,request,response):
        print('趙六')
        return response

     def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
         print('張欣彤')


from django.shortcuts import HttpResponse
class ROW2(MiddlewareMixin):
    def process_request(self,request):
        print("李四")
       

    def process_response(self,request,response):
        print('薛七')
        return response

     def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
         print('張須要')


class ROW3(MiddlewareMixin):
    def process_request(self,request):
        print("王五")

    def process_response(self,request,response):
        print('宋八')

        return response
     def process_view(self, request, veiw_func, veiw_func_args, veiw_func_kwargs):
         print('邵林')

    

views.py代碼與其最上的views.py代碼相同

則執行順序爲:朱三-->李四-->王五-->張欣彤-->張須要-->邵林-->小豬-->沒帶錢-->宋八-->薛七-->趙六

而模板中間件(函數中出現render才生效)和異常中間價(函數出現錯誤才生效)由於不經常使用,默認不生效,因此就不詳細介紹了。

3.自定義中間件


(1)建立中間件

class RequestExeute(object):
      
    def process_request(self,request):
        pass
    def process_view(self, request, callback, callback_args, callback_kwargs):
        i =1
        pass
    def process_exception(self, request, exception):
        pass
      
    def process_response(self, request, response):
        return response

(2)註冊中間件

MIDDLEWARE_CLASSES = (
    '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',
    'wupeiqi.middleware.auth.RequestExeute',
)
相關文章
相關標籤/搜索