初始化后是否可以更改`template_folder`?

时间:2013-10-08 12:02:29

标签: python flask config

使用这个Flask bootstrap project,我想指定一些必须在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对象),我该怎么做呢。

1 个答案:

答案 0 :(得分:2)

你可能会改变其中一些,但Flask并不是设计用于在创建应用程序后在应用程序上更改这些内容。只需查看一些代码,例如static route is created in the constructor of the Flask application

因此,您需要在构建时设置这些参数。好消息是您的配置通常以一种非常容易加载的方式完成(例如,在python模块中)。您应该能够将引导程序更改为:

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