Pylons uses Paste to deal with HTTP responses which has a thin wrapper class around WSGI. This wrapper class is available as paste.Response and is already imported to your lib/base.py module.
To set cookies or some response header you invoke corresponding method of WSGIResponse
instance. Starting from Pylons 0.9.6, there is a global response object you can modify directly. Pylons 0.9.7 uses WebOb
's Request and Response objects, which have slightly different methods than the Request and Response used in Pylons 0.9.6 and earlier.
1
2
3
4
5
6 | def setlang(self):
#'response' is the ``pylons.response`` global.
response.set_cookie('sitelang', 'uk', expires=3600) # Pylons 0.9.6 syntax.
#response.set_cookie('sitelang', 'uk', max_age=3600) # Pylons 0.9.7 syntax.
forward_to = request.headers.get('REFERER', '/')
redirect_to(forward_to)
|
This sets a cookie 'sitelang' with string value 'uk' which expires in an hour (3600 seconds).
Cookies can be read from the dictionary 'cookies' of the global request object in the next
request.
1 | lang = request.cookies['sitelang']
|