Flask: How to manage different environment databases?

后端 未结 5 1976
青春惊慌失措
青春惊慌失措 2020-12-13 11:27

I am working on an app which looks similar to

facebook/
         __init__.py
         feed/
             __init__.py
             business.py
             vi         


        
5条回答
  •  Happy的楠姐
    2020-12-13 11:47

    You can create a "config" module which contains the configuration for each environment. Thereafter the currently running environment can be specified by setting a shell variable.

    If you are initializing your flask application in the main init file, the configuration also could be set there. This is how I set my configuration:

    def setup_config(app):
        """Set the appropriate config based on the environment settings"""
        settings_map = {'development': DevelopmentSettings,
                        'staging': StagingSettings,
                        'testing': TestingSettings,
                        'production': ProductionSettings}
        env = environ['ENV'].lower()
        settings = settings_map[env]
        app.config.from_object(settings)
    

    Setting up environment variable before running the development server or even the tests can be a hassle, therefore I automate these actions with a makefile.

    Also take a look at flask-script http://flask-script.readthedocs.org/en/latest/.

提交回复
热议问题