Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Positional parameters

Lets say we would like to make some pretty URLs for some lists that we have on our site. We have a "show_list" method that takes a user and an id as parameters. The "normal" way would be to have http://ourdomain.com/show_list?user=amix&id=13. But with positional paramteres, this can be written http://ourdomain.com/show_list/amix/13:

import cherrypy

class Root:

    def show_list(self, user, id):
        return "user: %s <br /> id: %s" % (user, id)
    show_list.exposed = True

cherrypy.quickstart(Root())

This also works well when you don't know the number of path components beforehand. Just take advantage of Python's arbitrary arguments:

class Root:

    def view(self, *path):
        return ", ".join(["(%s)" % p for p in path])
    view.exposed = True

Older versions

replace this with this
2.2cherrypy.quickstart(Root())cherrypy.root = Root()
cherrypy.server.start()
2.0import cherrypyfrom cherrypy import cpg as cherrypy

You can read more about positional paramters for 2.2 in the CherryPy book (http://www.cherrypy.org/chrome/common/2.2/docs/book/chunk/index.html#id3326679)

CherryPy 2.1 allowed positional parameters for methods named "default".

CherryPy 2.0 had no allowance for positional parameters.

Hosted by WebFaction

Log in as guest/cpguest to create tickets