使用带有蓝图的主机参数(register_blueprint)的烧瓶



我似乎在注册蓝图时似乎无法获得匹配。参考以下问题 @miracle2k帖子以下代码:

if in_production:
    app.register_blueprint(shop, host='localhost:<port>')
    app.register_blueprint(info, host='info.localhost:<port>')

我尝试了类似的事情而没有运气。但是,当我在这样的路线中声明host时:

@foo.route('/', host='<host>.bar.com')
...
app.register_blueprint(foo)

主机路由正常工作。我宁愿在蓝图中声明主机,因此我不必在单一路线上拥有host=。有什么想法可能出了什么问题?

注意:在我的烧瓶应用程序中,我有Delcared app.url_map.host_matching = True

我刚刚在问题中发布了您提到的解决方案(要点),如果您实际上希望您的蓝图表现得好像没有主机匹配一样(仅指定主机)对于特定于主机的路线)。

如果您真的需要每个具有特定主机的蓝图,我会创建一个自定义的Blueprint类,该类从默认一个继承,并通过覆盖add_url_rule方法来按照您的方式行事:

class MyBlueprint(Blueprint):
    def __init__(self, *args, **kwargs):
        self.default_host = kwargs.pop('default_host', None)
        Blueprint.__init__(self, *args, **kwargs)
    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
        options['host'] = options.pop('host', self.default_host)
        super().add_url_rule(self, rule, endpoint=None, view_func=None, **options)
...
shop = Blueprint(..., default_host='localhost:<port>')
info = Blueprint(..., default_host='info.localhost:<port>')

最新更新