日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python?Django中間件詳細(xì)介紹_python

作者:許小許520 ? 更新時(shí)間: 2022-11-24 編程語言

一 .中間件簡介

中間件是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

欄目分類
最近更新