初始化后可以更改`template_folder`吗? [英] Is it possible to change `template_folder` after initialisation?

查看:516
本文介绍了初始化后可以更改`template_folder`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是代码的起始部分:

 app_name = app_name或__name__ 
app = Flask(app_name)
$ b $ config = config_str_to_obj(config)
configure_app(app,config)
configure_logger(app,config)
configure_blueprints(app,blueprints或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

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



我该怎么做没有努力写在这个会见他们hod(通过使用Config对象)。

解决方案

你可能会改变其中的一些,但是Flask不是旨在处理应用程序创建后更改这些应用程序。只是看一些代码,例如,静态路由是在Flask应用程序的构造函数中创建。



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

  app_name = app_name或__name__ 

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

configure_app(app,config)
...


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

解决方案

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

这篇关于初始化后可以更改`template_folder`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆