0

使用这个Flask 引导项目,我想指定一些必须在 Flask 对象创建中定义的参数(如 template_folder、static_url_path 和 static_path)

这是代码的开始部分:

app_name = app_name or __name__
app = Flask(app_name)

config = config_str_to_obj(config)
configure_app(app, config)
configure_logger(app, config)
configure_blueprints(app, blueprints or config.BLUEPRINTS)
configure_error_handlers(app)
configure_database(app)
configure_context_processors(app)
configure_template_filters(app)
configure_extensions(app)
configure_before_request(app)
configure_views(app)

return app

但是没有办法指定前面指出的参数。

我怎样才能做到这一点而不用这种方法硬写它们(例如通过使用 Config 对象)。

4

1 回答 1

2

You MIGHT be able to change some of these, but Flask is not designed to handle changing these on the app after the app is created. Just looking at some to the code, for example, the static route is created in the constructor of the Flask application.

So, you will need to set these parameters at build time. The good news is that your configuration is typically done in a way that can be pretty easily loaded (e.g., in a python module). You should be able to change your bootstrap to:

app_name = app_name or __name__

config = config_str_to_obj(config)
app = Flask(app_name, static_url_path=config.SOME_CONFIGURATION_NAME)

configure_app(app, config)
...
于 2013-10-08T13:25:07.187 回答