Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-2.0.0/cherrypy/tutorial/08_sessions.py

Revision 102 (checked in by dpotter, 4 years ago)

add descriptive names to the tutorials

Line 
1 """
2 Tutorial 08 - Sessions
3
4 Storing session data in CherryPy applications is very easy: cpg.request
5 provides a dictionary called sessionMap that represents the session
6 data for the current user. If you use RAM based sessions, you can store
7 any kind of object into that dictionary; otherwise, you are limited to
8 objects that can be pickled.
9 """
10
11 from cherrypy import cpg
12
13
14 class HitCounter:
15     def index(self):
16         # Increase the silly hit counter
17         count = cpg.request.sessionMap.get('count', 0) + 1
18
19         # Store the new value in the session dictionary
20         cpg.request.sessionMap['count'] = count
21
22         # And display a silly hit count message!
23         return '''
24             During your current session, you've viewed this
25             page %s times! Your life is a patio of fun!
26         ''' % count
27
28     index.exposed = True
29
30
31 cpg.root = HitCounter()
32
33 cpg.server.start(configFile = 'tutorial.conf')
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets