在一次请求的周期,可以在g中设置值,在本次的请求周期中都可以读取或复制。 相当于是一次请求周期的全局变量,也可以理解为是一个存放全局变量的空间或者类。

from flask import Flask,g

app = Flask(__name__,static_url_path='/xx')

@app.before_request
def f1():
    g.x1 = 123

@app.route('/index')
def index():
    print(g.x1)
    return 'hello world'

if __name__ == '__main__':
    app.run()

 

相关文章:

  • 2021-12-18
  • 2021-12-19
  • 2021-11-18
  • 2021-11-22
  • 2021-08-04
  • 2021-11-25
猜你喜欢
  • 2022-12-23
  • 2021-07-11
  • 2021-08-08
  • 2021-11-23
  • 2022-12-23
  • 2021-10-18
  • 2021-12-04
相关资源
相似解决方案