当我执行这个 url 时:
http://domain:8081/forum?id=2&page=26
使用此代码:
@route('/forum')
def display_forum():
forum_id = request.query.id
page = request.query.page or '1'
return template('Forum ID: {{id}} (page {{page}})', id=forum_id, page=page)
网页返回论坛ID:2(第26页)
我需要获得调用动态 rest url 的相同结果。
url 可以是 http://domain:8081/forum/2/26
或 http://domain:8081/forum/city/place/day/hour
。
不存在固定数量的参数。
我在 bottle 文档中看到了一些想法,可能类似于通配符过滤器 :path
。
最佳答案
它不会扩展到无限的路线,但像这样的东西会起作用。
@route('/forum/<first>')
def test(first):
return first
@route('/forum/<first>/<second>')
def test(first, second):
return first, second
@route('/forum/<first>/<second>/<third>')
def test(first, second, third):
return first, second, third
@route('/forum/<first>/<second>/<third>/<fourth>')
def test(first, second, third, fourth):
return first, second, third, fourth
关于python - 如何从 Bottle 中的路径获取参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363037/