Flask默认配置参数
方式一:字段赋值方式导入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from flask import Flask app = Flask(__name__) app.config[ "debug" ] = True app.secret_key = 'afadfadfaf' @app .route( "/" ) def index(): return "hello" if __name__ = = '__main__' : app.run() |
缺点:对于极少数字段配置可以使用,多字段配置不可取
方式二:py文件导入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# app.py<br>from flask import Flask app = Flask(__name__) app.config.from_pyfile( "settings.py" ) # 指定setting文件 @app .route( "/" ) def index(): return "hello" if __name__ = = '__main__' : app.run() # settings.py DEBUG = True ... |
方式三:python类路径方式导入
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from flask import Flask app = Flask(__name__) app.config.from_object( "test.settings.TestingConfig" ) # 系统环境需能找到的模块路径,默认在app同级目录下 @app .route( "/" ) def index(): return "hello" if __name__ = = '__main__' : app.run() |
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 |
推荐配置导入方式,可以根据开发环境灵活配置
默认配置参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
{ 'DEBUG' : get_debug_flag(default = False ), 是否开启Debug模式 'TESTING' : False , 是否开启测试模式 'PROPAGATE_EXCEPTIONS' : None , 'PRESERVE_CONTEXT_ON_EXCEPTION' : None , 'SECRET_KEY' : None , 'PERMANENT_SESSION_LIFETIME' : timedelta(days = 31 ), 'USE_X_SENDFILE' : False , 'LOGGER_NAME' : None , 'LOGGER_HANDLER_POLICY' : 'always' , 'SERVER_NAME' : None , 'APPLICATION_ROOT' : None , 'SESSION_COOKIE_NAME' : 'session' , 'SESSION_COOKIE_DOMAIN' : None , 'SESSION_COOKIE_PATH' : None , 'SESSION_COOKIE_HTTPONLY' : True , 'SESSION_COOKIE_SECURE' : False , 'SESSION_REFRESH_EACH_REQUEST' : True , 'MAX_CONTENT_LENGTH' : None , 'SEND_FILE_MAX_AGE_DEFAULT' : timedelta(hours = 12 ), 'TRAP_BAD_REQUEST_ERRORS' : False , 'TRAP_HTTP_EXCEPTIONS' : False , 'EXPLAIN_TEMPLATE_LOADING' : False , 'PREFERRED_URL_SCHEME' : 'http' , 'JSON_AS_ASCII' : True , 'JSON_SORT_KEYS' : True , 'JSONIFY_PRETTYPRINT_REGULAR' : True , 'JSONIFY_MIMETYPE' : 'application/json' , 'TEMPLATES_AUTO_RELOAD' : None , } |