烧瓶不加载配置 [英] Flask does not load configuration
问题描述
我在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.
我已经检查了config [config_name]等的类型.它们很好.
配置文件如下.导入对象类型没有问题.如果静态通过,一切正常. 'host'='serverip'是有意的.
此外,当我尝试使用SQLAlchemy连接到db时,不会出现此问题,但是在MongoDB的情况下,它不会在应用程序设置中更新MONGODB_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,
}
有趣.
app.config.update(MONGODB_SETTINGS={'DB':'testing_db'}) # works
settings = dict([('db', 'testing_db')])
app.config.update(MONGODB_SETTINGS=settings) # Does not work
此外,当我尝试使用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.
我认为问题可能出在您希望将app.config.from_object
传递给字符串时将Python对象提供给app.config.from_object
的事实.从文档中:
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
因此,在您的情况下,您可能需要执行以下操作:
app.config.from_object('your_app.config.{}'.format(config_name))
其中config_name
与config.py中的对象匹配.
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.
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.
这篇关于烧瓶不加载配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!