Flask does not load configuration

…衆ロ難τιáo~ 提交于 2019-12-12 15:10:45

问题


I am facing trouble with loading configuration in Flask.

from config import config, DevelopmentConfig, TestingConfig, ProductionConfig

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])    # Doesnot load configuration
    app.config.from_object(DevelopmentConfig)    # Loads configuration succesfully.

I have checked the type of config[config_name] etc. They are just fine.

config file is given as follow. There are no issues with import, object types. If passed statically everything works fine. 'host'='serverip' is intentional.

Also, this problem does not arise when I try to connect to db using SQLAlchemy but in case of MongoDB, it does not update MONGODB_SETTINGS in application settings.

import os
basedir = os.path.abspath(os.path.dirname(__file__))

from helper.helper_functions import generate_secret_key

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or generate_secret_key()
    SSL_DISABLE = False

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
   DEBUG = True
    MONGODB_SETTINGS = {
        'DB': 'development_db',
        'host': 'localhost',
       'port': 27017
    }


class TestingConfig(Config):
    TESTING = True
    WTF_CSRF_ENABLED = False
    MONGODB_SETTINGS = {
        'DB': 'testing_db',
        'HOST': 'localhost',
        'PORT': 27017
    }


class ProductionConfig(Config):
    MONGODB_SETTINGS = {
        'DB': 'production_db',
        'host': 'server_ip',
        'port': 27017,       # default =27017
        # other settings...
    }

    @classmethod
    def init_app(app):
        Config.init_app(app)

config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': TestingConfig,
}

Interestingly.

app.config.update(MONGODB_SETTINGS={'DB':'testing_db'})   # works
settings = dict([('db', 'testing_db')])
app.config.update(MONGODB_SETTINGS=settings)    # Does not work

Also, when I try to load configuration from configuration file using the other method offered by Flask-Config.

conf_name = 'test-config.py'
app.config.pyfile(conf_name)    # Doesnot load the configuration from the file.
app.config.pyfile(''+conf_name)    # Doesnot load the configuration from the file.
app.config.pyfile('test-config.py')    #successfully loads the configuration from file.

回答1:


I believe the problem may lie in the fact that you are giving a Python object to app.config.from_object when it expects a string. From the docs:

app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS')

http://flask.pocoo.org/docs/0.10/config/#configuring-from-files

So in your case you may want do something like:

app.config.from_object('your_app.config.{}'.format(config_name))

where config_name matches the object in your config.py.



来源:https://stackoverflow.com/questions/34811823/flask-does-not-load-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!