|
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 |
|
|---|
| 17 |
count = cpg.request.sessionMap.get('count', 0) + 1 |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
cpg.request.sessionMap['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 |
|
|---|
| 28 |
index.exposed = True |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
cpg.root = HitCounter() |
|---|
| 32 |
|
|---|
| 33 |
cpg.server.start(configFile = 'tutorial.conf') |
|---|