How to import from config file in Flask?

前端 未结 3 875
自闭症患者
自闭症患者 2020-12-23 02:13

I have followed the layout of my Flask project from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world.

I have the following structure:

3条回答
  •  粉色の甜心
    2020-12-23 02:50

    After a little bit of fiddling (and a little help from the 'net), I could improve this further, by changing the code to include the config to:

    app.config.from_object('config.ProductionConfig')
    

    This enables this cool pattern for configurations:

    class Config(object):
        DEBUG = True
        DEVELOPMENT = True
        SECRET_KEY = 'do-i-really-need-this'
        FLASK_HTPASSWD_PATH = '/secret/.htpasswd'
        FLASK_SECRET = SECRET_KEY
        DB_HOST = 'database' # a docker link
    
    class ProductionConfig(Config):
        DEVELOPMENT = False
        DEBUG = False
        DB_HOST = 'my.production.database' # not a docker link
    

    What's left now is to see how to integrate testing configs into this, but at least it feels less clumsy.

提交回复
热议问题