flask 设置配置文件的方式

from flask import Flask
from flask import current_app
"""
配置参数设置与读取
"""
app = Flask(__name__, static_url_path='/static', static_folder='static', template_folder='templates')

# 配置文件使用方式
# 方式一:从文件中配置参数
# app.config.from_pyfile('config.cfg')


# 方式二:从对象中配置参数
class Config(object):
    DEBUG = True
    ITCAST = "python"


# 传入类名称
app.config.from_object(Config)

# 方式三:直接操作app.config
# 配置设置都在app.config中,如果你的配置参数较少,可以直接设置在app.config中
# app.config['DEBUG'] = True


@app.route('/')
def hello_world():
    # 如果想在视图中使用配置文件中的内容,有两种方式
    # 方式一:直接从全局对象app的config字典中取值
    # 这种一般是视图所在的模块与app在同一个py文件, 视图函数能够操作的app
    print(app.config.get("ITCAST"))
    # 方式一:通过current_app获取参数
    # current_app 相当于app的一个代理, 与app一样
    print(current_app.config.get("ITCAST"))
    return 'Hello World!'


if __name__ == '__main__':
    # 关于run()的说明
    app.run()
    # app.run(host='0.0.0.0', port=5000, debug=True)  # 所有的ip都能访问
原文地址:https://www.cnblogs.com/yuqiangli0616/p/10342660.html