13.3 The context

This is what the context object is made of, but the programmer can set attributes to it to pass values around.

The context also provides an API.

13.3.1 The form values

The client may send data to the server either with the URI’s query, or within the request body, for example when the user submits a form. To access these values it is possible to use the request object, but it is strongly recommended to use the higher level API provided by the context:

Example: Hello buddy

To practice the API above we are going to see an slightly more elaborate example:

    class MyRoot(Root):

        GET__access__ = True
        def GET(self, context):
            name = context.get_form_value('name', default='World')
            return 'Hello %s' % name

Now, the URI http://localhost:8080 will return the same response as before, but http://localhost:8080/?name=buddy will give a customized message. You can try with other values to better appreciate the power of this code.

13.3.2 Cookies

Cookies can be used to implement client side sessions1, this is, to keep information across several requests. The context object provides a high level API to work with them:

13.3.3 Redirect

The context object offers this API for redirections:

Footnotes

  1. Note that itools.web does not provide and will never provide server side sessions, because they are bad, bad, bad.