網(wǎng)站首頁 編程語言 正文
一 .中間件簡介
中間件是Django請求/響應(yīng)處理的鉤子函數(shù)。它是一個(gè)輕量級的、低級的"插件系統(tǒng)",用于全局改變Django的輸入和輸出。中間件是幫助我們在視圖函數(shù)執(zhí)行之前都可以做一些額外的操作,它本質(zhì)就是一個(gè)自定義類,類中定義幾個(gè)方法,Djago框架會(huì)在特定的時(shí)候自動(dòng)觸發(fā)。
每個(gè)中間件負(fù)責(zé)做一些特定的功能。例如,‘django.contrib.auth.middleware.AuthenticationMiddleware’,它是用來將用戶與請求關(guān)聯(lián)起來。
二 .編寫自己的中間件
中間件工廠是一個(gè)可調(diào)用的程序,它接受請求并返回響應(yīng)。
中間件可以定義五個(gè)方法,分別是:
- process_request(self, request)
- process_response(self, request, response)
- process_view(self, request, view_func, view_args, view_kwargs)
- process_template_response(self,request,response)
- process_exception(self, request, exception)
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self, request):
print('來自M1 process_request')
class M2(MiddlewareMixin):
def process_request(self, request):
print('來自M2 process_request')
from django.utils.deprecation import MiddlewareMixin
class M1(MiddlewareMixin):
def process_request(self, request):
print('來自M1 process_request')
def process_response(self, request, response):
print('來自M1 process_response')
return response
class M2(MiddlewareMixin):
def process_request(self, request):
print('來自M2 process_request')
def process_response(self, request, response):
print('來自M2 process_response')
return response
- process_resquest
需要一個(gè)參數(shù)request,它的返回值可以是None,也可以是HttpResponse對象,如果返回None,就繼續(xù)向下走,返回HttpResponse對象就不執(zhí)行視圖函數(shù)直接將對象返回給瀏覽器。
在process_request方法中,視圖函數(shù)是最后執(zhí)行的,靠前的先執(zhí)行。
總結(jié):
- 中間件的process_request方法是在執(zhí)行視圖函數(shù)之前執(zhí)行的。
- 當(dāng)配置多個(gè)中間件時(shí),會(huì)按照列表的索引值,從上到下依次執(zhí)行。
- 不同的中間件之間傳遞的request是同一個(gè)對象。
- process_response
在執(zhí)行完視圖函數(shù)之后才執(zhí)行,執(zhí)行順序和process_request方法相反,從下往上依次執(zhí)行。
定義process_response方法時(shí),需要傳參request和response,response是視圖函數(shù)返回的HttpResponse對象,如果返回一個(gè)自己定義的HttpResponse對象,瀏覽器接收到的也是自定義的而不是后端處理的。
- process_view
process_view(self, request, view_func, view_args, view_kwargs)
該方法有四個(gè)參數(shù)
request是HttpRequest對象。
view_func是Django即將使用的視圖函數(shù)。 (它是實(shí)際的函數(shù)對象,而不是函數(shù)的名稱作為字符串。)
view_args是將傳遞給視圖的位置參數(shù)的列表.
view_kwargs是將傳遞給視圖的關(guān)鍵字參數(shù)的字典。 view_args和view_kwargs都不包含第一個(gè)視圖參數(shù)(request)。
Django會(huì)在調(diào)用視圖函數(shù)之前調(diào)用此方法,返回None或者HttpResponse對象,如果返回None,Django將繼續(xù)處理這個(gè)請求,執(zhí)行其他中間件的process_view方法,然后執(zhí)行視圖函數(shù)。如果返回HttpResponse對象,那么就不執(zhí)行視圖函數(shù),而是直接掉頭,執(zhí)行process_response方法返回給瀏覽器。
process_view方法是在Django路由系統(tǒng)之后,視圖系統(tǒng)之前執(zhí)行的,執(zhí)行順序按照MIDDLEWARE中的注冊順序從前到后順序執(zhí)行的。
- process_exception
process_exception(self, request, exception)
該方法兩個(gè)參數(shù):
一個(gè)HttpRequest對象
一個(gè)exception是視圖函數(shù)異常產(chǎn)生的Exception對象。
這個(gè)方法只有在視圖函數(shù)中出現(xiàn)異常了才執(zhí)行,它返回的值可以是一個(gè)None也可以是一個(gè)HttpResponse對象。如果是HttpResponse對象,Django將調(diào)用模板和中間件中的process_response方法,并返回給瀏覽器,否則將默認(rèn)處理異常。如果返回一個(gè)None,則交給下一個(gè)中間件的process_exception方法來處理異常。它的執(zhí)行順序也是按照中間件注冊順序的倒序執(zhí)行。
- process_template_response
process_template_response(self, request, response)
它的參數(shù),一個(gè)HttpRequest對象,response是TemplateResponse對象(由視圖函數(shù)或者中間件產(chǎn)生)。
process_template_response是在視圖函數(shù)執(zhí)行完成后立即執(zhí)行,但是它有一個(gè)前提條件,那就是視圖函數(shù)返回的對象有一個(gè)render()方法(或者表明該對象是一個(gè)TemplateResponse對象或等價(jià)方法)。
三 .中間件執(zhí)行流程
當(dāng)有請求來時(shí),先按照前后順序依次執(zhí)行process_request()方法,process_request如果返回None,就往下執(zhí)行,如果返回一個(gè)HttpResponse對象就不執(zhí)行process_request,而是執(zhí)行當(dāng)前層級的process_response方法,依次向上執(zhí)行,返回給瀏覽器。例如下圖中在中間件3返回了HttpResponse對象,就執(zhí)行中間件3的process_response,依次往上返回給瀏覽器。下面的process_request,process_response都不會(huì)執(zhí)行。
process_request方法都執(zhí)行完,找到對應(yīng)的視圖函數(shù),先不執(zhí)行視圖函數(shù),而是執(zhí)行中間件中的process_view方法,process_view返回None就繼續(xù)依次執(zhí)行,所有的process_view執(zhí)行完才執(zhí)行視圖函數(shù)。下圖中的中間件3返回了HttpResponse對象,下面的process_view方法就不執(zhí)行了,直接執(zhí)行最下邊的process_response方法,依次向上返回給瀏覽器。
process_template_response和process_exception兩個(gè)方法的觸發(fā)是有條件的,執(zhí)行順序也是倒序。
原文鏈接:https://blog.csdn.net/weixin_68531269/article/details/126837098
相關(guān)推薦
- 2022-09-22 k8s 配置存儲(chǔ)之 Configmap & secret
- 2021-12-16 Docker快速部署SpringBoot項(xiàng)目介紹_docker
- 2024-03-02 前端directus對接單點(diǎn)登錄
- 2022-09-18 Go語言實(shí)現(xiàn)服務(wù)端消息接收和發(fā)送_Golang
- 2022-10-08 Golang?Web?框架Iris安裝部署_Golang
- 2022-12-28 Qt開發(fā)之QTreeWidget的使用教程詳解_C 語言
- 2022-05-22 C++的拷貝構(gòu)造函數(shù)你了解嗎_C 語言
- 2022-07-23 C#操作windows系統(tǒng)進(jìn)程的方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支