Will
Will

Reputation: 1189

How do I create request handlers dynamically in CherryPy?

How would one go about creating request handler's at runtime with CherryPy? The default dispatch method shows creating the handler objects explicitly and building the tree that way:

class OnePage(object):
    def index(self):
        return "one page!"
    index.exposed = True

class HelloWorld(object):
    onepage = OnePage()

    def index(self):
        return "hello world"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

This is fine assuming your URL's are hard-coded. But what about user defined URLs? Is it possible to create the tree at runtime?

The reason I'm asking is I'd like to create a CMS on top of CherryPy where users can specify their own URL schemes. Eg. http://example.com/my/custom/url

Or would it be better to use the root index as a catch-all and simply process the url parameters that way?

Upvotes: 2

Views: 1518

Answers (1)

cyraxjoe
cyraxjoe

Reputation: 5741

You can use routes, if that's your thing, or build a root object with the default-dispatcher approach, I personally use the default routing, is more natural, and goes along with the growing of the code, but some people feel more comfortable with the notion of separated logic from routing to application, your choice.

Upvotes: 5

Related Questions