|
Revision 727
(checked in by fumanchu, 3 years ago)
|
Tutorial fixes, plus a bug in _cputil.getErrorPage.
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Tutorial - Sessions |
|---|
| 3 |
|
|---|
| 4 |
Storing session data in CherryPy applications is very easy: cherrypy.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 |
import cherrypy |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
class HitCounter: |
|---|
| 15 |
def index(self): |
|---|
| 16 |
|
|---|
| 17 |
count = cherrypy.session.get('count', 0) + 1 |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
cherrypy.session['count'] = count |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 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 |
index.exposed = True |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
cherrypy.root = HitCounter() |
|---|
| 31 |
cherrypy.config.update({'sessionFilter.on': True}) |
|---|
| 32 |
|
|---|
| 33 |
if __name__ == '__main__': |
|---|
| 34 |
cherrypy.config.update(file = 'tutorial.conf') |
|---|
| 35 |
cherrypy.server.start() |
|---|
| 36 |
|
|---|