有沒有一種方法可以在Flask中使用可選的URL參數定義URL?從本質上講,我想要做的就是定義規則,允許隨意指定的語言:可選的URL變量
/
/de -> matches/(but doesn't collide with /profile)
/profile
/de/profile
我想我已經想出了一個辦法做到這一點,但它涉及要麼進行更改如何WERKZEUG和Flask處理請求(猴子修補或分叉框架源)。這似乎是一個過於複雜的方式來處理這個問題,雖然..有沒有更容易的方法來做到這一點,我俯瞰?
編輯:
基於Brian的回答,這裏就是我想出了:
app.py:
from loc import l10n
def create_app(config):
app = Flask(__name__)
app.config.from_pyfile(config)
bp = l10n.Blueprint()
bp.add_url_rule('/', 'home', lambda lang_code: lang_code)
bp.add_url_rule('/profile', 'profile', lambda lang_code: 'profile: %s' %
lang_code)
bp.register_app(app)
return app
if __name__ == '__main__':
create_app('dev.cfg').run()
LOC/l10ln.py
class Blueprint(Blueprint_):
def __init__(self):
Blueprint_.__init__(self, 'loc', __name__)
def register_app(self, app):
app.register_blueprint(self, url_defaults={'lang_code': 'en'})
app.register_blueprint(self, url_prefix='/<lang_code>')
self.app = app
(我還沒有得到來自變量列表拉lang_code
尚未,但將這樣做不久)
現在這只是熱恕我直言。
肯定是邁向imho的方式,考慮到藍圖 –