转自:http://heipark.iteye.com/blog/1746187
1. 直接配置
app.config['HOST']='xxx.a.com'
print app.config.get('HOST')
2. 通过环境变量加载配置
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
# default_config.py
HOST = 'localhost'
PORT = 5000
DEBUG = True
4. 通过文件配置
app.config.from_pyfile('default_config.py') # 这里defualt_config.py是文件
<pre name="code" class="python"># default_config.py
HOST = 'localhost'
PORT = 5000
DEBUG = True
#使用配置
<pre name="code" class="python">print app.config['HOST']