有没有一种方法可以在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的方式,考虑到蓝图 –