Help me better understand CherryPy PageHandlers

Say I have 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 press the url http: // localhost: 8080 / names / , I see Names.index: No, this is normal, This means that the Names () class is being called.

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

This bothers me because, according to the PageHandler documentation:

When the request is processed, the URI is broken down into its components, and each of them is mapped in order with the nodes in the tree. Any final components are components of the "virtual path" and are passed as positional arguments.

Now let's say that I modify the Names () class as follows:

 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 is going on here?

+4
source share
1 answer

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

+3
source

All Articles