我正在使用Flask 0.9。
现在我想将三个urls路由到同一个函数:
/item/<int:appitemid>
/item/<int:appitemid>/
/item/<int:appitemid>/<anything can be here>
<anything can be here>
部件永远不会在函数中使用。
为了实现这个目标,我必须将同一个函数复制两次:
@app.route('/item/<int:appitemid>/')
def show_item(appitemid):
@app.route('/item/<int:appitemid>/<path:anythingcanbehere>')
def show_item(appitemid, anythingcanbehere):
会有更好的解决方案吗?
发布于 2012-12-25 00:32:32
为什么不使用一个可能为空的参数,其默认值为None
@app.route('/item/<int:appitemid>/')
@app.route('/item/<int:appitemid>/<path:anythingcanbehere>')
def show_item(appitemid, anythingcanbehere=None):
发布于 2012-12-25 00:33:17
Yes -使用以下结构:
@app.route('/item/<int:appitemid>/<path:path>')
@app.route('/item/<int:appitemid>', defaults={'path': ''})
请参阅http://flask.pocoo.org/snippets/57/上的代码片段
https://stackoverflow.com/questions/14023664
复制相似问题