Is it possible to change `template_folder` after i

2019-09-16 09:56发布

问题:

Using this Flask bootstrap project, I'd like to specify some parameters that must be defined in the Flask object creation (like template_folder, static_url_path and static_path)

Here's the starting part of the code :

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

But there is no way to specify the parameters indicated before.

How can I do that without hard writing them in this method (by using the Config object for example).

回答1:

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)
...