flask---闪现/请求扩展/g对象

闪现

复制代码
# 一个请求---》假设出错了---》重定向到另一个地址---》把错误信息在另一个返回中看到
错误信息放个位置----》另一个请求过来,去那个位置拿
# 把一些数据,放在某个位置---》后期可以去取出来----》取完不用删除,就没了
def index():
    s='xx错位了'
    return redirect('/errors')

def errors():
    return index.html



# 如何设置
flash('aaa')
# 如何取
get_flashed_message()

# 分类放
flash('超时错误', category="x1")
# 分类取
data = get_flashed_messages(category_filter=['x1'])

from flask import Flask,flash,get_flashed_messages

app=Flask(__name__)
app.debug=True
app.secret_key='asdfasdfasdfa'
# 只要用闪现---》就要设置secret_key----》放到session中了

@app.route('/')
def index():
    # 放入我的名字
    # flash('lqz')  # 放哪了?

    # 分类放
    flash('超时错误', category="x1")
    return 'xxx'
@app.route('/home')
def home():
    # 取出我的名字
    name=get_flashed_messages(category_filter=['x2'])
    print(name)
    return 'home'

@app.route('/order')
def order():
    # 取出我的名字
    # name=get_flashed_messages()
    # print(name)
    data = get_flashed_messages(category_filter=['x1'])
    print(data)
    # 按分类取
    return 'home'
if __name__ == '__main__':
    app.run(port=8080)

# 1 django 的message就是做这个事的
# 2 闪现有什么用?
	-暂时存数据
    -当次请求出错了,下次请求要显示这个错误

请求扩展

复制代码
# 类似于django中的中间件
	-请求来了,请求走了,做一些拦截
        1 before_request
        2 after_request
        3 before_first_request
        4 teardown_request
        5 errorhandler
        6 template_global
        7 template_filter
        
        
# before_request 每个请求来了,都会走

before_request

复制代码
# 请求来了执行---》process_request--->可以下多个,从上往下依次执行
@app.before_request
def before():
    # 当次请求的对象就是request
    # 1  判断访问的不同路径
    # if request.path=='/':
    #     print('我来了')
    # else:
    #     print('访问其他路径')
    # 2 判断如果是跟路径,向request对象中放个name,其他不放
    # if request.path == '/':
    #     request.name = 'lqz'
    # else:
    #     print('访问其他路径')

    # 3 返回None,会执行下一个请求扩展,如果返回4件套,直接就返回了,不再往下执行了
    # if request.path=='/':
    #     return '不让你访问'
    # else:
    #     print('访问其他路径')
    # 4 多个before_request,从上往下执行
    print('1111')


@app.before_request
def before2():
    print('2222')

after_request

复制代码
@app.after_request
def after2(response):
    print('走了走了222')
    return response


@app.route('/')
def index():
    # print(request.name)
    return 'xxx'

before_first_request

复制代码
# 项目运行起来的第一次会执行,以后再也不执行了
# 1.x可以,2.x弃用了

teardown_request

复制代码
# teardown_request 每一个请求之后绑定一个函数,即使遇到了异常---》一般用来记录日志--->不能做统一异常处理
@app.teardown_request
def ttt(e):
    # 通过判断e是否为空,记录日志
    print(e)  # 出了异常,e就是异常对象,如果没有异常,e就是None
    print('来了老弟')
    if e:
        return '出异常了'

errorhandler

复制代码
## errorhandler
@app.errorhandler(404)
def error(e):
    print(e)
    print('xxx')
    return render_template('error.html')


@app.errorhandler(500)
def error1(e):
    print(e)
    print('500')
    return jsonify({'code': 999, 'msg': '服务器内部错误,请联系系统管理员'})

template_global全局标签

复制代码
# template_global 全局标签,所有页面都可以用
@app.template_global()
def add(a1, a2):
    return a1 + a2

template_filter全局过滤器

复制代码
# template_filter 全局过滤器
@app.template_filter()
def db(a1, a2, a3):
    print(a1)
    return a1 + a2 + a3

g对象

复制代码
# 请求来了,在request中放个path,后续的视图函数中都能拿到
# flask不建议向request对象中放变量,建议使用g对象

# g对象,当次请求中放入的变量,在当次请求中一直存在

# global的缩写,global


# g和session的区别
	-g只针对于当次请求
    -session可以跨请求
相关推荐
心中有国也有家3 分钟前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy
卷毛的技术笔记1 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥1 小时前
匿名函数 lambda + 高阶函数
java·python·算法
vb2008111 小时前
FastAPI APIRouter
开发语言·python
会编程的土豆2 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
adrninistrat0r2 小时前
Java调用链MCP分析工具
java·python·ai编程
喵个咪2 小时前
GoWind Toolkit Go后端代码生成 完整全流程实战
后端·go·orm
杨充2 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
basketball6162 小时前
Go 语言从入门到进阶:4. 数组和MAP使用方法总结
开发语言·后端·golang
qq_2518364572 小时前
SpringBoot+Vue 共享电池柜管理系统 完整实现 前后端分离项目实战 完整代码
vue.js·spring boot·后端