g对象
g对象就是一个容器,能够多次取值
from flask import Flask,g,request
app = Flask(__name__)
@app.before_request
def be():
#request.name="cxw is big"
if request.path=="/login":
g.name="cxw is big"
def set_g():
g.name = "cxw is big"
@app.route("/")
def index():
set_g()
print("视图1",g.name)
# print("视图2", g.name)
return "ok"
@app.route("/login")
def login():
print("login视图1", g.name)
return "login"
@app.after_request
def af(response):
print("after_request", g.name)
return response
if __name__ == '__main__':
app.run()
原文地址:https://www.cnblogs.com/chanyuli/p/12153162.html
时间: 2024-11-09 06:16:27