tags:

views:

108

answers:

1

Let's say I have some code (using CherryPy) that looks like this:

import cherrypy

class Names:
    def index(self, name=None):
        return "Names.index:  " + str(name)
    index.exposed = True

class Root:
    def index(self):
        return "This is the root"
    index.exposed = True

if __name__ == "__main__":
    root = Root()

    root.names = Names()

    cherrypy.tree.mount(root, '/')
    cherrypy.engine.start()
    cherrypy.engine.block()

If I hit the url http://localhost:8080/names/, I see Names.index: None, which is fine. That means the Names() class is being called.

But, if I go to http://localhost:8080/names/mark, I get a 404 Error instead of the Names.index: mark I was expecting.

This confuses me because, according to the PageHandler documentation:

When a request is processed, the URI is split into its components, and each one is matched in order against the nodes in the tree. Any trailing components are "virtual path" components and are passed as positional arguments.

Now let's say I change the Names() class to look like this:

class Names:
    def index(self, name=None):
        return "Names.index:  " + str(name)
    index.exposed = True

    def name(self, name=None):
        return "Names.name:  " + str(name)
    name.exposed = True

Now I can go to http://localhost:8080/names/name/mark and I see Names.name: mark.

Can someone explain what's happening here?

+3  A: 

The index method is an exception to the partial matching rules. You can use the default method instead, or in this particular example make names a method itself.

Coady
Excellent. Thanks for the info.
Mark Biek