Table Of Contents

Using Cookies in TurboGears

TurboGears uses the CherryPy method of setting and reading cookies, which is to say it uses Python’s Cookie.SimpleCookie object.

Accessing Cookies in Templates

If you want to read or write cookies in your templates (though doing so might be considered a violation of of the MVC pattern), you can add the simple_cookie object as a custom template variable:

from turbogears.view import variable_providers

def add_cookie(vars):
    vars['cookie'] = cherrypy.response.simple_cookie


Now the simple_cookie object will be available as tg.cookie in all your templates. The page Global Template Variables has more about custom template variables in the section “Custom Additions”.