Django中間件簡介
django 中的中間件(middleware),在django中,中間件其實(shí)就是一個(gè)類盟戏,在請(qǐng)求到來和結(jié)束后,django會(huì)根據(jù)自己的規(guī)則在合適的時(shí)機(jī)執(zhí)行中間件中相應(yīng)的方法晚碾。
在django項(xiàng)目的settings模塊中抓半,有一個(gè) MIDDLEWARE_CLASSES 變量,其中每一個(gè)元素就是一個(gè)中間件
中間件中一共有五個(gè)方法:
process_request(self,request)
process_view(self, request, callback, callback_args, callback_kwargs)
process_exception(self, request, exception)
process_response(self, request, response)
process_template_response(self,request,response)
只有在視圖函數(shù)的返回對(duì)象中有render方法才會(huì)執(zhí)行格嘁!默認(rèn)不執(zhí)行
并把對(duì)象的render方法的返回值返回給用戶(注意不返回視圖函數(shù)的return的結(jié)果了笛求,而是返回視圖函數(shù) return值(對(duì)象)的render方法)
中間件之process_request,process_response
process_request(self,request)
process_response(self, request, response)
當(dāng)用戶發(fā)起請(qǐng)求的時(shí)候會(huì)依次經(jīng)過所有的的中間件,這個(gè)時(shí)候的請(qǐng)求時(shí)process_request,最后到達(dá)views的函數(shù)中探入,views函數(shù)處理后狡孔,在依次穿過中間件,
這個(gè)時(shí)候是process_response,最后返回給請(qǐng)求者
在django中叫中間件蜂嗽,在其他web框架中苗膝,有的叫管道,httphandle
上述截圖中的中間件都是django中的植旧,我們也可以自己定義一個(gè)中間件辱揭,我們可以自己寫一個(gè)類,但是必須繼承MiddlewareMixin
所以需要導(dǎo)入:from django.utils.deprecation import MiddlewareMixin
我們?cè)陧?xiàng)目文件下創(chuàng)建一個(gè)Middle目錄病附,并在下面創(chuàng)建m1.py代碼例子如下:
復(fù)制代碼
"""AUTHOR:FAN"""
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class Row1(MiddlewareMixin):
def process_request(self,request):
print("中間件1請(qǐng)求")
def process_response(self,request,response):
print("中間件1返回")
return response
class Row2(MiddlewareMixin):
def process_request(self,request):
print("中間件2請(qǐng)求")
# return HttpResponse("走")
def process_response(self,request,response):
print("中間件2返回")
return response
class Row3(MiddlewareMixin):
def process_request(self,request):
print("中間件3請(qǐng)求")
def process_response(self,request,response):
print("中間件3返回")
return response
復(fù)制代碼
這樣當(dāng)頁面發(fā)起請(qǐng)求的時(shí)候:后臺(tái)效果如下
中間件1請(qǐng)求
中間件2請(qǐng)求
中間件3請(qǐng)求
中間件3返回
中間件2返回
中間件1返回
但是如果當(dāng)請(qǐng)求到達(dá)請(qǐng)求2的時(shí)候直接不符合條件返回问窃,程序?qū)烧?qǐng)求直接發(fā)給中間件2返回,然后依次返回到請(qǐng)求者
用如下圖進(jìn)行理解:
當(dāng)然這是在django1.10的時(shí)候完沪,在之前的版本的時(shí)候是直接返回到最后一個(gè)中間件的response,然后向上依次返回域庇,最后到發(fā)起請(qǐng)求
中間件之process_view
process_view(self, request, callback, callback_args, callback_kwargs)
我們?cè)趍1.py文件中的的代碼進(jìn)行更改:
復(fù)制代碼
"""AUTHOR:FAN"""
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class Row1(MiddlewareMixin):
def process_request(self,request):
print("中間件1請(qǐng)求")
def process_response(self,request,response):
print("中間件1返回")
return response
def process_view(self, request, callback, callback_args, callback_kwargs):
print("中間件1view")
class Row2(MiddlewareMixin):
def process_request(self,request):
print("中間件2請(qǐng)求")
# return HttpResponse("走")
def process_response(self,request,response):
print("中間件2返回")
return response
def process_view(self, request, callback, callback_args, callback_kwargs):
print("中間件2view")
class Row3(MiddlewareMixin):
def process_request(self,request):
print("中間件3請(qǐng)求")
def process_response(self,request,response):
print("中間件3返回")
return response
def process_view(self, request, callback, callback_args, callback_kwargs):
print("中間件3view")
復(fù)制代碼
高亮部分為添加的內(nèi)容,這樣運(yùn)行之后效果如下:
中間件1請(qǐng)求
中間件2請(qǐng)求
中間件3請(qǐng)求
中間件1view
中間件2view
中間件3view
中間件3返回
中間件2返回
中間件1返回
我們通過下圖進(jìn)行分析上面的過程:
當(dāng)最后一個(gè)中間的process_request到達(dá)路由關(guān)系映射之后覆积,返回到中間件1的process_view听皿,然后依次往下,到達(dá)views函數(shù)宽档,最后通過process_response依次返回到達(dá)用戶
中間件之process_exception
process_exception(self, request, exception)
當(dāng)views的函數(shù)中出現(xiàn)錯(cuò)誤時(shí)尉姨,就會(huì)執(zhí)行process_exception方法
如果在中間中添加了process_exception方法,工作圖示為:
這樣當(dāng)用戶發(fā)起請(qǐng)求的時(shí)候到達(dá)中間件3的process_request之后會(huì)到達(dá)urls路由關(guān)系映射這里吗冤,如果匹配到了就會(huì)到中間件1的process_view,
然后依次傳遞到中間件3的process_view,到達(dá)view函數(shù)啊送。
如果view函數(shù)中有報(bào)錯(cuò),則會(huì)從中間件3依次 欣孤,
這里即中間件3的process_response,然后依次返回到用戶昔逗,如果沒有匹配到這個(gè)錯(cuò)誤則直接在頁面顯示錯(cuò)誤信息降传。如果view函數(shù)中沒有錯(cuò)誤,
則到中間3即最后一個(gè)中間件3的process_response勾怒,然后依次向上婆排,傳到用戶
中間件之process_template_responseprocess
process_template_response(self,request,response)
只有當(dāng)views函數(shù)中返回的對(duì)象中具有render方法,是就會(huì)直接process_template_responseprocess
所有的努力都值得期許笔链,每一份夢想都應(yīng)該灌溉段只!