[Twisted-Python] twisted.web, url -> resource mapping and isLeaf questions
![](https://secure.gravatar.com/avatar/0c95cd90aef753bc89f1c48370eeb8b7.jpg?s=120&d=mm&r=g)
Hi, I am following "twisted in 60sec" series, but even the simple examples are a bit unclear to me, especially w.r.t. url mapping. For example: # assume the right imports... class MainPage(Resource): isLeaf = True def render_GET(self, request): return "somestring" resource = MainPage() factory = Site(resource) reactor.listenTCP(8880, factory) reactor.run() I was expecting that accessing any other 'path' besides the main url would cause 404, e.g. "http://localhost:8880/foo/bar", and I get the same page instead. I gather this is a consequence of the isLeaf setting set to be True, because twisted.web stops as soon as it encounters MainPage in the hierarchy and use that for rendering. Setting it to False seems to avoid the page to be available at all. The solution I got instead is something like: class RootPage(Resource): isLeaf = False def render(self, request): return "somestring" def getChild(self, path, request): if path: return NoResource() else: return self and adding new resources through putChild. Isn't there a more natural way of doing things ? cheers, David
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
On Nov 30, 2010, at 1:07 AM, David wrote:
Isn't there a more natural way of doing things ?
No. I'm not sure what you're asking here; it seems like you've figured out the resource model. It has some rough edges, which we are trying to eventually fix (see, for example, <http://tm.tl/3621>). But the things you're talking about are just how it works; <http://example.com/> is a collection, just like <http://example.com/foo/>, so it behaves in a consistent way. (This message may be more appropriate for the twisted-web list.)
![](https://secure.gravatar.com/avatar/0c95cd90aef753bc89f1c48370eeb8b7.jpg?s=120&d=mm&r=g)
On 11/30/2010 05:42 PM, Glyph Lefkowitz wrote:
I'm not sure what you're asking here; it seems like you've figured out the resource model.
It was not obvious to me I figured it out, I guess :)
I did not know there was a twisted.web specific list - will refer to that list from now on, cheers, David
![](https://secure.gravatar.com/avatar/e1554622707bedd9202884900430b838.jpg?s=120&d=mm&r=g)
On Nov 30, 2010, at 1:07 AM, David wrote:
Isn't there a more natural way of doing things ?
No. I'm not sure what you're asking here; it seems like you've figured out the resource model. It has some rough edges, which we are trying to eventually fix (see, for example, <http://tm.tl/3621>). But the things you're talking about are just how it works; <http://example.com/> is a collection, just like <http://example.com/foo/>, so it behaves in a consistent way. (This message may be more appropriate for the twisted-web list.)
![](https://secure.gravatar.com/avatar/0c95cd90aef753bc89f1c48370eeb8b7.jpg?s=120&d=mm&r=g)
On 11/30/2010 05:42 PM, Glyph Lefkowitz wrote:
I'm not sure what you're asking here; it seems like you've figured out the resource model.
It was not obvious to me I figured it out, I guess :)
I did not know there was a twisted.web specific list - will refer to that list from now on, cheers, David
participants (2)
-
David
-
Glyph Lefkowitz