django 前后端分離,Django 框架篇(七) : 中間件 以及 5種方法

 2023-10-15 阅读 30 评论 0

摘要:中間件介紹: ? 官方的說法:中間件是一個用來處理Django的請求和響應的框架級別的鉤子。它是一個輕量、低級別的插件系統,用于在全局范圍內改變Django的輸入和輸出。每個中間件組件都負責做一些特定的功能。 但是由于其影響的是全局,所以需要謹慎使用&#x

中間件介紹:

?

官方的說法:中間件是一個用來處理Django的請求和響應的框架級別的鉤子。它是一個輕量、低級別的插件系統,用于在全局范圍內改變Django的輸入和輸出。每個中間件組件都負責做一些特定的功能。

但是由于其影響的是全局,所以需要謹慎使用,使用不當會影響性能。

說的直白一點中間件是幫助我們在視圖函數執行之前和執行之后都可以做一些額外的操作,它本質上就是一個自定義類,類中定義了幾個方法,Django框架會在處理請求的特定的時間去執行這些方法。

我們一直都在使用中間件,只是沒有注意到而已,打開Django項目的Settings.py文件,看到下圖的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',
]
復制代碼
復制代碼

django 前后端分離,MIDDLEWARE配置項是一個列表,列表中是一個個字符串,這些字符串其實是一個個類,也就是一個個中間件。

我們之前已經接觸過一個csrf相關的中間件了?我們一開始讓大家把他注釋掉,再提交post請求的時候,就不會被forbidden了,后來學會使用csrf_token之后就不再注釋這個中間件了。

那接下來就學習中間件中的方法以及這些方法什么時候被執行。

?

自定義中間件 的五個方法:

?

中間件可以定義五個方法,分別是:(主要的是process_request和process_response)

  • process_request(self,request)
  • process_view(self, request, view_func, view_args, view_kwargs)
  • process_template_response(self,request,response)
  • process_exception(self, request, exception)
  • process_response(self, request, response)

django中間件?以上方法的返回值可以是None或一個HttpResponse對象,如果是None,則繼續按照django定義的規則向后繼續執行,如果是HttpResponse對象,則直接將該對象返回給用戶。

?

?

自定義一個中間件示例

復制代碼
復制代碼
from django.utils.deprecation import MiddlewareMixinclass MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return response
復制代碼
復制代碼

?

process_request:

process_request有一個參數,就是request,這個request和視圖函數中的request是一樣的。

它的返回值可以是None也可以是HttpResponse對象。返回值是None的話,按正常流程繼續走,交給下一個中間件處理,如果是HttpResponse對象,Django將不執行視圖函數,而將響應對象返回給瀏覽器。

django常用模塊,我們來看看多個中間件時,Django是如何執行其中的process_request方法的。

復制代碼
復制代碼
from django.utils.deprecation import MiddlewareMixinclass MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")class MD2(MiddlewareMixin):def process_request(self, request):print("MD2里面的 process_request")
復制代碼
復制代碼

在settings.py的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','middlewares.MD1',  # 自定義中間件MD1'middlewares.MD2'  # 自定義中間件MD2
]
復制代碼
復制代碼

此時,我們訪問一個視圖,會發現終端中打印如下內容:

MD1里面的 process_request
MD2里面的 process_request
app01 中的 index視圖

把MD1和MD2的位置調換一下,再訪問一個視圖,會發現終端中打印的內容如下:

MD2里面的 process_request
MD1里面的 process_request
app01 中的 index視圖

看結果我們知道:視圖函數還是最后執行的,MD2比MD1先執行自己的process_request方法。

在打印一下兩個自定義中間件中process_request方法中的request參數,會發現它們是同一個對象。

django框架。由此總結一下:

  1. 中間件的process_request方法是在執行視圖函數之前執行的。
  2. 當配置多個中間件時,會按照MIDDLEWARE中的注冊順序,也就是列表的索引值,從前到后依次執行的。
  3. 不同中間件之間傳遞的request都是同一個對象

?

process_response:

它有兩個參數,一個是request,一個是response,request就是上述例子中一樣的對象,response是視圖函數返回的HttpResponse對象。該方法的返回值也必須是HttpResponse對象。

給上述的M1和M2加上process_response方法:

復制代碼
復制代碼
from django.utils.deprecation import MiddlewareMixinclass MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return responseclass MD2(MiddlewareMixin):def process_request(self, request):print("MD2里面的 process_request")def process_response(self, request, response):print("MD2里面的 process_response")return response
復制代碼
復制代碼

訪問一個視圖,看一下終端的輸出:

MD2里面的 process_request
MD1里面的 process_request
app01 中的 index視圖
MD1里面的 process_response
MD2里面的 process_response

看結果可知:

django框架的運行流程。process_response方法是在視圖函數之后執行的,并且順序是MD1比MD2先執行。(此時settings.py中 MD2比MD1先注冊)

多個中間件中的process_response方法是按照MIDDLEWARE中的注冊順序倒序執行的,也就是說第一個中間件的process_request方法首先執行,而它的process_response方法最后執行,最后一個中間件的process_request方法最后一個執行,它的process_response方法是最先執行。

?

process_view

process_view(self, request, view_func, view_args, view_kwargs)

該方法有四個參數

request是HttpRequest對象。

django前后端不分離。view_func是Django即將使用的視圖函數。 (它是實際的函數對象,而不是函數的名稱作為字符串。)

view_args是將傳遞給視圖的位置參數的列表.

view_kwargs是將傳遞給視圖的關鍵字參數的字典。 view_args和view_kwargs都不包含第一個視圖參數(request)。

Django會在調用視圖函數之前調用process_view方法。

它應該返回None或一個HttpResponse對象。 如果返回None,Django將繼續處理這個請求,執行任何其他中間件的process_view方法,然后在執行相應的視圖。 如果它返回一個HttpResponse對象,Django不會調用適當的視圖函數。 它將執行中間件的process_response方法并將應用到該HttpResponse并返回結果。

?給MD1和MD2添加process_view方法:

復制代碼
復制代碼
from django.utils.deprecation import MiddlewareMixinclass MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD1 中的process_view")print(view_func, view_func.__name__)class MD2(MiddlewareMixin):def process_request(self, request):print("MD2里面的 process_request")def process_response(self, request, response):print("MD2里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD2 中的process_view")print(view_func, view_func.__name__)
復制代碼
復制代碼

django中間件的五個方法、訪問index視圖函數,看一下輸出結果:

復制代碼
復制代碼
MD2里面的 process_request
MD1里面的 process_request
--------------------------------------------------------------------------------
MD2 中的process_view
<function index at 0x000001DE68317488> index
--------------------------------------------------------------------------------
MD1 中的process_view
<function index at 0x000001DE68317488> index
app01 中的 index視圖
MD1里面的 process_response
MD2里面的 process_response
復制代碼
復制代碼

process_view方法是在process_request之后,視圖函數之前執行的,執行順序按照MIDDLEWARE中的注冊順序從前到后順序執行的

?

process_exception:

process_exception(self, request, exception)

該方法兩個參數:

一個HttpRequest對象

django框架主要用來做什么。一個exception是視圖函數異常產生的Exception對象。

這個方法只有在視圖函數中出現異常了才執行,它返回的值可以是一個None也可以是一個HttpResponse對象。如果是HttpResponse對象,Django將調用模板和中間件中的process_response方法,并返回給瀏覽器,否則將默認處理異常。如果返回一個None,則交給下一個中間件的process_exception方法來處理異常。它的執行順序也是按照中間件注冊順序的倒序執行。

?給MD1和MD2添加上這個方法:

復制代碼
復制代碼
from django.utils.deprecation import MiddlewareMixinclass MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD1 中的process_view")print(view_func, view_func.__name__)def process_exception(self, request, exception):print(exception)print("MD1 中的process_exception")class MD2(MiddlewareMixin):def process_request(self, request):print("MD2里面的 process_request")def process_response(self, request, response):print("MD2里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD2 中的process_view")print(view_func, view_func.__name__)def process_exception(self, request, exception):print(exception)print("MD2 中的process_exception")
復制代碼
復制代碼

如果視圖函數中無異常,process_exception方法不執行。

想辦法,在視圖函數中拋出一個異常:

def index(request):print("app01 中的 index視圖")raise ValueError("呵呵")return HttpResponse("O98K")

在MD1的process_exception中返回一個響應對象:

復制代碼
復制代碼
class MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD1 中的process_view")print(view_func, view_func.__name__)def process_exception(self, request, exception):print(exception)print("MD1 中的process_exception")return HttpResponse(str(exception))  # 返回一個響應對象
復制代碼
復制代碼

django后端。看輸出結果:

復制代碼
復制代碼
MD2里面的 process_request
MD1里面的 process_request
--------------------------------------------------------------------------------
MD2 中的process_view
<function index at 0x0000022C09727488> index
--------------------------------------------------------------------------------
MD1 中的process_view
<function index at 0x0000022C09727488> index
app01 中的 index視圖
呵呵
MD1 中的process_exception
MD1里面的 process_response
MD2里面的 process_response
復制代碼
復制代碼

注意,這里并沒有執行MD2的process_exception方法,因為MD1中的process_exception方法直接返回了一個響應對象。

?

process_template_response(用的比較少)

?

process_template_response(self, request, response)

它的參數,一個HttpRequest對象,response是TemplateResponse對象(由視圖函數或者中間件產生)。

第七框架計劃?process_template_response是在視圖函數執行完成后立即執行,但是它有一個前提條件,那就是視圖函數返回的對象有一個render()方法(或者表明該對象是一個TemplateResponse對象或等價方法)。

復制代碼
復制代碼
class MD1(MiddlewareMixin):def process_request(self, request):print("MD1里面的 process_request")def process_response(self, request, response):print("MD1里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD1 中的process_view")print(view_func, view_func.__name__)def process_exception(self, request, exception):print(exception)print("MD1 中的process_exception")return HttpResponse(str(exception))def process_template_response(self, request, response):print("MD1 中的process_template_response")return responseclass MD2(MiddlewareMixin):def process_request(self, request):print("MD2里面的 process_request")def process_response(self, request, response):print("MD2里面的 process_response")return responsedef process_view(self, request, view_func, view_args, view_kwargs):print("-" * 80)print("MD2 中的process_view")print(view_func, view_func.__name__)def process_exception(self, request, exception):print(exception)print("MD2 中的process_exception")def process_template_response(self, request, response):print("MD2 中的process_template_response")return response
復制代碼
復制代碼

views.py中:

復制代碼
復制代碼
def index(request):print("app01 中的 index視圖")def render():print("in index/render")return HttpResponse("O98K")rep = HttpResponse("OK")rep.render = renderreturn rep
復制代碼
復制代碼

訪問index視圖,終端輸出的結果:

復制代碼
復制代碼
MD2里面的 process_request
MD1里面的 process_request
--------------------------------------------------------------------------------
MD2 中的process_view
<function index at 0x000001C111B97488> index
--------------------------------------------------------------------------------
MD1 中的process_view
<function index at 0x000001C111B97488> index
app01 中的 index視圖
MD1 中的process_template_response
MD2 中的process_template_response
in index/render
MD1里面的 process_response
MD2里面的 process_response
復制代碼
復制代碼

從結果看出:

視圖函數執行完之后,立即執行了中間件的process_template_response方法,順序是倒序,先執行MD1的,在執行MD2的,接著執行了視圖函數返回的HttpResponse對象的render方法,返回了一個新的HttpResponse對象,接著執行中間件的process_response方法。

中間件的執行流程

上一部分,我們了解了中間件中的5個方法,它們的參數、返回值以及什么時候執行,現在總結一下中間件的執行流程。

七下知識框架。請求到達中間件之后,先按照正序執行每個注冊中間件的process_reques方法,process_request方法返回的值是None,就依次執行,如果返回的值是HttpResponse對象,不再執行后面的process_request方法,而是執行當前對應中間件的process_response方法,將HttpResponse對象返回給瀏覽器。也就是說:如果MIDDLEWARE中注冊了6個中間件,執行過程中,第3個中間件返回了一個HttpResponse對象,那么第4,5,6中間件的process_request和process_response方法都不執行,順序執行3,2,1中間件的process_response方法。

?

process_request方法都執行完后,匹配路由,找到要執行的視圖函數,先不執行視圖函數,先執行中間件中的process_view方法,process_view方法返回None,繼續按順序執行,所有process_view方法執行完后執行視圖函數。假如中間件3 的process_view方法返回了HttpResponse對象,則4,5,6的process_view以及視圖函數都不執行,直接從最后一個中間件,也就是中間件6的process_response方法開始倒序執行。

process_template_response和process_exception兩個方法的觸發是有條件的,執行順序也是倒序。總結所有的執行流程如下:

?

django前后端分離。

轉載于:https://www.cnblogs.com/123zzy/p/9800727.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/138694.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息