方法一、直接配置
app.config['HOST']='xxx.a.com'
print app.config.get('HOST')
方法二、通过环境变量加载配置
export MyAppConfig=/path/to/settings.cfg
app.config.from_envvar('MyAppConfig')
这样将会加载环境变量指向的配置文件,并加载
方法三、通过对象加载
class Config(object):
DEBUG = False
TESTING = False
DATABASE_URI = 'sqlite://:memory:'
class ProductionConfig(Config):
DATABASE_URI = 'mysql://user@localhost/foo'
class DevelopmentConfig(Config):
DEBUG = True
class TestingConfig(Config):
TESTING = True
from flask import Flask
app = Flask(__name__)
app.config.from_object(ProductionConfig)
print app.config.get('DATABASE_URI') # mysql://user@localhost/foo
或者:
from flask import Flask
import default_config
app = Flask(__name__)
app.config.from_object(default_config) # 这里 defualt_config是一个对象
print app.config.get('HOST')
# default_config.py
HOST = 'localhost'
PORT = 5000
DEBUG = True
方法四、通过配置文件
app.config.from_pyfile('default_config.py') # 这里defualt_config.py是文件
# default_config.py
HOST = 'localhost'
PORT = 5000
DEBUG = True
HOST = 'localhost'
PORT = 5000
DEBUG = True
# 使用配置
print app.config['HOST']
--end