2016-11-21 30 views
1

註冊藍圖時,我似乎無法獲得域匹配工作。談到following issue @ miracle2k帖子下面的代碼:使用主機參數與藍圖(register_blueprint)

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=。任何想法可能是錯誤的?

注意:在我的Flask應用程序中,我已經挖掘了app.url_map.host_matching = True

回答

0

我剛剛在提到solution (gist)的問題中發佈了這個問題,如果您確實希望您的藍圖的行爲好像沒有主機匹配(並且僅指定主機的特定主機路由),那麼可能會對您有所幫助。

如果你真的需要你的藍圖各有特定的主機,我將突出部分創建一個自定義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>')