flask提供了多种配置加载方式。
可以通过app.config.直接看源码
代码样例:
from flask import Flask
app = Flask(__name__)
class DefaultConfig(object):
SECRET_KEY = "12313132"
# 一般会创建类做封装
def create_flask_app(config):
app = Flask(__name__) # 启动主模块
# 自定义转换器添加至flask中
app.config.from_object(config)
# app.config.from_envvar('asda')
return app
app = create_flask_app(DevelopmentConfig)
全局变量–容器g
from flask import Flask,Blueprint, current_app,g
app = Flask(__name__)
def test_GG():
return g.uname + g.userId
@app.route('/g')
def test_g():
g.userId = "1231312"
g.uname = "zhangshan"
res = test_GG()
return res