[Welcome] [TitleIndex] [WordIndex


You want to record a unique piece of information on every user of a Quixote application.


Use the set_cookie() method of the HTTPResponse object.

     get_response().set_cookie('login_time', str(time.time()), path='/')

This will set a cookie named "login_time" to the value of the current time. Cookies can only have string values. The 'path' attribute means that the cookie will be included with all HTTP requests to any path on your server; you can limit it to a subtree of your URL space by specifying something like path='/customer'.


See CookieSecurity for a discussion of security considerations related to cookies.

Note that some users will have cookies disabled. Usually I don't worry too much about this; if people have cookies permanently disabled, many web applications will be broken for them and they'll be used to things not working. You may feel differently.

Additional attributes of cookies, such as the 'path', 'expires' and 'secure' attributes, can be specified as additional keyword arguments to set_cookie(). Consult 2109 for a full list of cookie attributes. The most commonlly used attribute is 'expires'; note that the expiration date is given in a 822-style date format that looks like "Weekday, DD-Mon-YY HH:MM:SS GMT". To set a permanent cookie you therefore need to do:

get_response().set_cookie('permanent_cookie', 'value', expires="Thu 01-Jan-2020 00:00:00 GMT")

Other cookbook recipes describe ReadingCookies and DeletingCookies.

If you want to manage session-specific information for users, you don't need to implement it all yourself; Quixote's session management can handle it and save you some effort.


2010-09-22 22:14