문제

I understand and have gotten RESTful routes working in my application using this guide http://docs.cherrypy.org/dev/progguide/REST.html

Does anyone know how to add a second RESTful resource nested within a first?

I'm expecting my code to look something like this, but I can't get it to work

import cherrypy

class Pets:
  exposed = True

  def GET(self, personID, petID):
    pass # GET /people/123/pets/333 return pet

  def POST(self, personID):
    pass # POST /people/123/pets create pet

class People:
  pets = Pets()

  exposed = True

  def GET(self, personID):
    pass # GET /people/123 return person

  def POST(self):
    pass # POST /people create person


config = {
  '/people': {
    'request.dispatch': cherrypy.dispatch.MethodDispatcher()
  }
}

cherrypy.tree.mount(.., '/', config)
도움이 되었습니까?

해결책

See the help docs for cherrypy.popargs. It pops path components, and supplies them as keyword arguments to the next handler. In this case, use it as a decorator on the people resource, and attach a pets resource to the people resource.

@cherrypy.popargs('petID')
class Pets:
    ...
@cherrypy.popargs('personID')
class People:
    ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top