日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python - Django - 中间件 process_exception

發(fā)布時間:2023/12/20 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python - Django - 中间件 process_exception 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

process_exception(self, request, exception) 函數(shù)有兩個參數(shù),exception 是視圖函數(shù)異常產(chǎn)生的 Exception 對象

process_exception 函數(shù)的執(zhí)行順序是按照 settings.py 中設(shè)置的中間件的順序的倒序執(zhí)行

process_exception 函數(shù)只在視圖函數(shù)中出現(xiàn)異常的時候才執(zhí)行,它返回的值可以是 None,也可以是一個 HttpResponse 對象

如果返回 None,則繼續(xù)由下一個中間件的 process_exception 方法來處理異常

如果返回 HttpResponse,將調(diào)用中間件中的 process_response 方法

middleware_test.py:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

from django.utils.deprecation import MiddlewareMixin from django.shortcuts import HttpResponse, renderclass Test(MiddlewareMixin):def process_request(self, request):print("這是一個中間件111 --> test11")def process_exception(self, request, exception):print("這里是 Test1的 process_exception")print(exception)return render(request, "index.html")class Test2(MiddlewareMixin):def process_request(self, request):print("這是一個中間件 --> test2")def process_exception(self, request, exception):print("這里是 Test2 的 process_exception")print(exception)

views.py:

1

2

3

4

5

6

7

from?django.shortcuts?import?HttpResponse

?

?

def?index(request):

????print("這里是 index 頁面")

????raise?ValueError("這是一個錯誤")

????return?HttpResponse("這里是主頁面 index")

訪問,http://127.0.0.1:8000/two/index/

?

總結(jié)

以上是生活随笔為你收集整理的Python - Django - 中间件 process_exception的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。