[Twisted-web] I feel like I have a path one-off error here somewhere...

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Aug 27 13:39:51 EDT 2009

On 05:24 pm, markscottwright at gmail.com wrote:
>I'm trying to set up a dynamic tree of resources - a list of resources
>that drill-down into individual resources.  But for some reason,
>getChild never gets called on my root resource.  The following code
>will output a list of ids, but "/id" doesn't call getChild.  What am I
>doing wrong?
>from twisted.web import server, resource
>class Child(resource.Resource):
>    def __init__(self, childid):
>        self.childid = childid
>    def render_GET(self, request):
>        return "<h1>" + self.childid + "</h1>"
>class Root(resource.Resource):
>    def render_GET(self, request):
>        pg = ""
>        for i in range(10):
>            pg += '<p><a href="/' + str(i) + '">' + str(i) + "</a></p>"
>        return pg
>    def getChild(self, path, request):
>        return Child(path)
>if __name__ == "__main__":
>    from twisted.internet import reactor
>    root = resource.Resource()
>    root.putChild('', Root())

With this resource hierarchy set up, here's what you should expect:

  - / will call root.getChild("")
  - /id will call root.getChild("id")
  - /foo/bar will call root.getChild("foo").getChild("bar")

So, perhaps you want to make root an instance of Root instead of an 
instance of Resource with a "" child of a Root instance?


More information about the Twisted-web mailing list