【问题标题】:Cherrypy index dispatcher not like definedCherrypy 索引调度程序不像定义的那样
【发布时间】:2013-08-15 14:43:28
【问题描述】:

这是我第一次使用 CherryPy,请原谅我的愚蠢行为。

我正在尝试编写一个 RESTful API,部分处理添加/删除人员。我希望能够 GET/PUT/DELETE example.com/people/。

对于索引方法和定义的函数,调度程序的行为似乎完全不同:

class people:
    """This is the class for CherryPy that deals with CRUD on people"""
    @cherrypy.expose
    def index(self, name):
        return name

    @cherrypy.expose
    def who(self, name):
        return name

root = webroot()
root.people = people()
cherrypy.quickstart(root)

如果我调用 example.com/people/tom,我会得到 404,如果我调用 example.com/people/who/tom,我会得到 'tom' 返回。

谁能看到我做错了什么?有没有办法可以将 /xxx 传递给索引?

【问题讨论】:

    标签: python rest cherrypy


    【解决方案1】:

    在 URL 参数方面,索引有点不同。

    索引方法在 CherryPy 中有一个特殊的作用:它处理以斜杠结尾的中间 URI;例如,URI /orders/items/ 可能映射到 root.orders.items.index。如果请求包含查询字符串或 POST 参数,则 index 方法可以采用额外的关键字参数;请参阅关键字参数,下一个。但是,与所有其他页面处理程序不同,它不能接受位置参数

    source

    但是,example.com/people?name=tom 的 url 应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      • 2020-01-20
      • 2012-10-30
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多