flask设置配置的的方式有三种
方法一:直接在代码中使用app.config[‘配置名’] 的方式
app.debug = True 等价于 app.config[‘debug’] = True,但是只要少部分参数支持app.debug = True的方式
from flask import Flask
app = Flask(__name__)
# flask 配置文件
app.debug = True
# app.config['debug'] = True
app.secret_key = "password"
# app.config['secret_key'] = "password"
@app.route('/')
def index():
return 'Hellow World!'
if __name__ == '__main__':
app.run(
方法二:使用 app.config.from_pyfile(“python文件名称”) 读取一个py文件的方式导入
创建一个settings.py 文件内容如下,配置文件的变量名都必须是大写的
DEBUG = True
再调用代码中输入如下
app.config.from_pyfile("settings.py")
print(app.config['DEBUG'])
方式三:app.config.from_object(“python类或类的路径”)
创建一个 settings.py文件,内容如下
class Config(object):
DEBUG = False
TESTING = False
DATABASE_URI = 'sqlite://:memory:'
class ProductionConfig(Config):
DATABASE_URI = 'mysql://user@localhost/foo'
class DevelopmentConfig(Config):
DEBUG = True
class TestingConfig(Config):
TESTING = True
在代码中导入settings.py 文件
app.config.from_object("settings.ProductionConfig")
@app.route('/config'/)
def config():
return app.config['DATABASE_URI']
settings.py文件默认要放在项目根路径下