Flask配置文件

flask的几种配置文件方式

方式一:放在app对象上,本质会放在 app.config 里面

app.secret_key = 'asdfsdf'
app.debug = False

方式二:放在app对象的config参数上

app.config['DEBUG'] = True
app.config['HOST'] = '127.0.0.1' 
print(app.config)

方式三:通过py文件设置 settings.py

app.config.from_pyfile('./settings.py')  # 会加载settings中得大写的配置,都放到 config 中
print(app.config)

方式四:使用环境变量---dot-env使用

app.config.from_envvar("环境变量名称")

方式五:常用,通过类

# app.config.from_object('settings.ProductionConfig')
app.config.from_object('settings.DevelopmentConfig')
print(app.config)

类配置

class Config(object):
    DEBUG = False
    TESTING = False
    SECRET_KEY='123'
    DATABASE_URI = '127.0.0.1'


class ProductionConfig(Config):
    DEBUG = False
    SECRET_KEY = 'asfasfd.ea33435asfasf'
    DATABASE_URI = '192.168.1.23'


class DevelopmentConfig(Config):
    DEBUG = True
    DATABASE_URI = '127.0.0.1'

方式六:其他

app.config.from_json("json文件名称")
# JSON文件名称,必须是json格式,因为内部会执行json.loads
app.config.from_mapping({'DEBUG': True})

posted @ 2024-02-28 16:35  wellplayed  阅读(205)  评论(0)    收藏  举报