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

root/trunk/cherrypy/tutorial/tut07_sessions.py

Revision 1837 (checked in by fumanchu, 8 months ago)

Fix for #756 (Deprecate server.quickstart):

  • server.quickstart now does nothing but raise a warning.
  • Made 'root' argument to cherrypy.quickstart optional (to make tutorials easier, but it applies broadly).
  • Removed all calls to server.quickstart.
  • Property svn:eol-style set to native
Line 
1 """
2 Tutorial - Sessions
3
4 Storing session data in CherryPy applications is very easy: cherrypy
5 provides a dictionary called "session" 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 import cherrypy
12
13
14 class HitCounter:
15    
16     _cp_config = {'tools.sessions.on': True}
17    
18     def index(self):
19         # Increase the silly hit counter
20         count = cherrypy.session.get('count', 0) + 1
21        
22         # Store the new value in the session dictionary
23         cherrypy.session['count'] = count
24        
25         # And display a silly hit count message!
26         return '''
27             During your current session, you've viewed this
28             page %s times! Your life is a patio of fun!
29         ''' % count
30     index.exposed = True
31
32
33 cherrypy.tree.mount(HitCounter())
34
35
36 if __name__ == '__main__':
37     import os.path
38     thisdir = os.path.dirname(__file__)
39     cherrypy.quickstart(config=os.path.join(thisdir, 'tutorial.conf'))
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets