CherryPy Project Download

Positional Parameter demonstration

Any exposed method defined in your application can make use of positional parameters, although it's extremely hard to explain in text. The easiest way to get a grip on how CherryPy deals with URL to parameter mapping is to create a very simple method, fire up your application, and try some URL's.

An example of this testing method could be as follows:

    @expose()
    def mytest(self, param_1=None, param_2=None, *args, **kw):
        return dict(param_1=param_1,
                    param_2=param_2,
                    args=args,
                    kw=kw)

Now, start your application. You can send some of the following URL's, and any others you can come up with, to see how the method behaves:

http://localhost:8080/mytest/foo/bar?baz=testing

http://localhost:8080/mytest/foo/bar/baz

http://localhost:8080/mytest/foo/bar?param_1=baz

You'll notice that the last URL will cause a 500 error. This is because the first part of the URL after /mytest (i.e. 'foo') is given as the value for param_1 and you are also passing param_1=baz using the URL parameter. Effectively you are defining param_1 twice which is just not very useful.

The conclusion to the above example is simply this: Take care when naming your method's parameters so they will not overlap with explicit keyword parameters.

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages