網站首頁 編程語言 正文
Flask高級_內置信號
一.介紹
Flask中常用的內置信號:
-
template_rendered:模版渲染完成后的信號。
-
before_render_template:模版渲染之前的信號。
-
request_started:請求開始之前,在到達視圖函數之前發送信號。
-
request_finished:請求結束時,在響應發送給客戶端之前發送信號。
-
request_tearing_down:請求對象被銷毀時發送的信號,即使在請求過程中發生異常也會發送信 號。
-
got_request_exception:在請求過程中拋出異常時發送信號,異常本身會通過exception傳遞到訂 閱(監聽)的函數中。一般可以監聽這個信號,來記錄網站異常信息。
-
appcontext_tearing_down:應用上下文被銷毀時發送的信號。
-
appcontext_pushed:應用上下文被推入到棧上時發送的信號。
-
appcontext_popped:應用上下文被推出棧時發送的信號。
-
message_flashed:調用了Flask的 flash 方法時發送的信號。
二.實例
template_rendered的使用:
#coding=utf-8
from flask import Flask,render_template,template_rendered
app = Flask(__name__)
@app.route('/')
def index():
return render_template('home.html')
def renderfunc(sender,template,context):
print(sender)
print(template)
print(context)
template_rendered.connect(renderfunc)
if __name__ == '__main__':
app.run(debug=True)
got_request_exception的使用:
#coding=utf-8
from unicodedata import name
from flask import Flask,got_request_exception
app = Flask(__name__)
@app.route('/')
def zero_err():
x = 1 / 0
return 'Hello~'
def errhandle(sender,exception):
with open('text.log','a',encoding='utf-8') as f:
f.write(str(sender) + '\n')
f.write(str(exception) + '\n')
got_request_exception.connect(errhandle)
if __name__ == '__main__':
app.run(debug=True)
注:
如果覺得筆記有些問題,麻煩在百忙之中在評論中指正,或提出建議!另外,如果覺得這份筆記對你有所幫助,麻煩動動發財的小手手點一波贊!
原文鏈接:https://blog.csdn.net/qq_55961861/article/details/126592627
相關推薦
- 2022-03-16 ASP.NET?Core在Linux下為dotnet創建守護進程_基礎應用
- 2022-05-10 在 VSCode 中如何設置默認的瀏覽器為Chrome或Firefox
- 2022-04-18 uniapp中使用拷貝,復制粘貼功能,uniapp,隱藏軟鍵盤
- 2022-08-18 詳解python?一維、二維列表的初始化問題_python
- 2022-08-28 ERROR 1366 (HY000): Incorrect string value: ‘\xE8\
- 2023-02-25 Golang內存泄漏場景以及解決方案詳析_Golang
- 2023-10-09 所有的引用類型都有自由可拓展性怎么理解
- 2023-07-24 el-table文字根據首字母排序
- 最近更新
-
- 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同步修改后的遠程分支