这是我第一次与CherryPy一起出去,所以原谅任何愚蠢。
我正在尝试编写一个RESTful API,它部分涉及添加/删除人员。我希望能够获得/ 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我会'汤姆'回来。
谁能看到我做错了什么?有没有办法可以将/ xxx传递给索引?
答案 0 :(得分:2)
关于URL参数,索引有点不同。
索引方法在CherryPy中有一个特殊的作用:它处理以斜杠结尾的中间URI;例如,URI / orders / items /可能映射到root.orders.items.index。如果请求包含querystring或POST params,则index方法可以使用其他关键字参数;接下来看关键字参数。但是,与所有其他页面处理程序不同,它不能采用位置参数
但是,example.com/people?name=tom
的网址应该按预期工作。