python - 如何从 Bottle 中的路径获取参数?

标签 python rest parameters path bottle

当我执行这个 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/26http://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/

相关文章:

spring - 使用 junit 测试端点(POST 方法)

angularjs - 在网页中提供 REST-API 数据而不暴露 API 端点

web-services - RESTful 部分更新返回 205 "Reset Content"?

c++ 这个参数在命令行中是什么意思? | grep MATCH >zout匹配

c++ - (C++) 与结构和函数参数有关的简单问题

python - 当我的 Mac 使用 python 2.7 时,为什么 Python.h 在我的/usr/local/python2.6 文件夹中?

python - 如何列出类定义中的所有函数调用?

python - 覆盖身份验证方法 - Django admin

javascript - 多态参数javascript

python - 如何在 TensorFlow 中获取 tf.summary.FileWriter 的文件名?