Table Of Contents

Using Sessions in TurboGearsΒΆ

To use sessions in TurboGears, you must first enable sessions in your config files (dev.cfg, prod.cfg) by adding this line to the global section:

session_filter.on = True

To demonstrate sessions, we’ll build a simple counter:

import cherrypy

def counter(self):
    # Session variable initialization (or recall if exists)
    cherrypy.session['count'] = cherrypy.session.get('count', 0)

    # Variable assignment
    cherrypy.session['count'] = cherrypy.session['count'] + 1

    #Return the value to your template
    return dict(counter=cherrypy.session['count'])

And the counter kid template (nothing special here, just for convenience):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xmlns:py=""
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/>
        <title>Counters in TurboGears</title>
        <div id="header">&nbsp;</div>
        <div id="main_content">
            <div id="counter_block">The count is: ${counter}</div>

Refreshing the page should cause the counter to increase.

The CherryPy site offers additional details of session configuration.