網站首頁 編程語言 正文
(1)@app.before_request
請求到達視圖函數之前,進行自定義操作,類似django中間件中的process_request,在app中使用則為全局,在藍圖中使用則針對當前藍圖
注意正常狀態下return值必須為None
(2)@app.after_request
響應返回到達客戶端之前,進行自定義操作,類似jango中間件中的process_response,在app中使用則為全局,在藍圖中使用則針對當前藍圖
注意正常狀態下視圖函數必須定義一個形參接收response對象,并通過return response返回
(3)@app.errorhandler()
錯誤狀態碼捕獲執行函數,裝飾器參數務必是4xx或者5xx的int型錯誤狀態碼
(4) @app.template_global() :定義裝飾全局模板可用的函數,直接可在模板中進行渲染使用
@app.template_filter(): 定義裝飾全局模板可用的過濾器函數,類似django中的自定義過濾器,直接可在模板中使用
這兩個特殊裝飾器主要用在模板渲染!!!
import apps
from flask import request, session, redirect
app = apps.create_app()
@app.before_request
def before1():
print("before1", request)
@app.before_request
def before2():
print("before2")
if request.path == "/":
return None
else:
#這里拋出了一個異常,會被@app.errorhandler(Exception)
# 捕獲到。
raise Exception("hahaha")
@app.before_request
def before3():
print("before3")
@app.after_request
def after1(res):
print("after1")
return res
@app.after_request
def after2(res):
print("after2")
return res
@app.after_request
def after3(res):
print("after3")
return res
# 處理異常,接受參數,可以重定向到指定頁面
@app.errorhandler(Exception)
def error(e):
print("error")
return redirect("/")
@app.route("/login")
def login():
print("login")
return "login"
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
if __name__ == '__main__':
app.run()
原文鏈接:https://blog.csdn.net/wtl1992/article/details/129187432
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-15 C語言實現字符串的部分匹配算法
- 2022-08-19 Spring Cloud Config配置服務
- 2022-03-13 .NET中IoC框架Autofac用法講解_自學過程
- 2023-12-25 fiddler展示接口的響應時間
- 2022-06-11 適合初學者的C語言常量類型的講解_C 語言
- 2022-12-12 封裝flutter狀態管理工具示例詳解_Android
- 2022-07-16 Linux查看日志的幾種方式
- 2022-08-27 Oracle數據庫存儲過程的調試過程_oracle
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支