Python Flask 特殊装饰器,执行路由前和执行路由后执行的装饰器

前言全局说明

Python Flask 特殊装饰器,执行路由前和执行路由后执行的装饰器


一、安装flask模块

官方源:

pip3 install flask==2.3.2

国内源:

pip3 install flask==2.3.2 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

以上二选一,哪个安装快用哪个
flask 安装时间 2023-11

更多国内源: https://www.cnblogs.com/wutou/p/17949398


二、引用模块

from flask import Flask

三、启动服务

https://www.cnblogs.com/wutou/p/17949220


四、特殊装饰器,在路由前和后执行的装饰器

html 不用多看,这次主要看命令行特殊装饰器执行前后的提示。
特殊装饰器用在,访问网页时,请求路由前先执行的函数,比如登录验证

4.1.2文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
@app.before_request
def bef():
print('before_request')
@app.after_request
def aft(i):
print('after_request')
return i
@app.route('/')
def index():
print('index')
if request.method == 'GET':
return render_template('index_ts.html', content = 'Hello Flask')
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)

注意:
before_request 装饰器里不要有return ,如果有return 则会提前放回,而不执行请求的路由
after_request 装饰器则是要必须有 参数 和 return
》 before_request 只在服务启动后,一个请求连接执行一次,而不是后面每个请求连接都执行。

4.1.2 文件名:index.html
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
</head>
<body>
{{ content }}
</body>
</html>
4.2 访问连接:

http://127.0.0.1:5000

4.3 效果:

图中可以看到,index 是网页执行的路由,
在index 之前执行了 before 装饰器内容;
在 inxdex之后又执行了 after 装饰器内容

image


五、多个特殊装饰器,执行顺序

5.1.1 文件名:index.py
from flask import Flask, render_template, request
app=Flask(__name__)
@app.before_request
def bef_1():
print('before_request_1')
@app.before_request
def bef():
print('before_request_2')
@app.after_request
def aft_1(i):
print('after_request_1')
return i
@app.after_request
def aft_2(i):
print('after_request_2')
return i
@app.route('/')
def index():
print('index')
if request.method == 'GET':
return render_template('index_ts.html', content = 'Hello Flask')
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)
5.1.2 文件名:index.html
同 4.1.2 (略)
5.2 访问连接:

http://127.0.0.1:5000

5.3 效果:

注意: before 是按代码先后顺序来执行的;而 after 是倒序执行的(这是源码里设置的)

image


五、多个特殊装饰器,执行顺序

5.1.1 文件名:index.py
from flask import Blueprint, render_template, request
b = Blueprint(__name__, 'x')
@b.before_request
def bef_1():
print('before_request_1')
@b.after_request
def aft_1(i):
print('after_request_1')
return i
@b.route('/')
def index():
print('index')
if request.method == 'GET':
return render_template('index_ts.html', content = 'Hello Flask')
if __name__ == '__main__':
# app.debug = True
# app.run(host='127.0.0.1',port = 5000)
app.run(host='0.0.0.0',port = 5000)

以上代码不能正常执行,只是为了说明
当使用蓝图创建对象的时候,那么 before 和 after 只在蓝图的作用域里有效,就是 b 的范围内有效




免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
https://www.bilibili.com/video/BV11Y411h71J?p=32
https://www.bilibili.com/video/BV11Y411h71J?p=56 09:30



posted @   悟透  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2022-01-11 Python模块之xlutils - 写入excel的xls文件
2022-01-11 Python模块之xlwt -写入excel的xls文件
2022-01-11 Python模块之xlrd - 读取excel的xls文件
点击右上角即可分享
微信分享提示