session is another instance of the Storage class. Whatever is stored into session for example:
1 session.myvariable = "hello"
can be retrieved at a later time:
1 a = session.myvariable as long as the code is executed within the same session by the same user (provided the user has not deleted session cookies and the session did not expire). Because session is a Storage object, trying to access an attribute/key that has not been set does not raise an exception; it returns None instead. The session object has two important methods. One is forget:
It tells web2py not to save the session. This should be used in those controllers whose actions are called often and do not need to track user activity.
The other method is connect:
1 session.connect(request, response, db, masterapp=None)
where db is the name of an open database connection (as returned by the DAL). It tells web2py that you want to store the sessions in the database and not on the filesystem. web2py creates a table:
1 db.define_table( 'web2py_session',
2 Field('locked', 'boolean', default=False),
4 Field('created_datetime', 'datetime', default=now),
5 Field( 'modified_datetime', 'datetime'),
and stores cPickled sessions in the session_data field.
The option masterapp=None, by default, tells web2py to try to retrieve an existing session for the application with name in request.application, in the running application.
If you want two or more applications to share sessions, set masterapp to the name of the master application.
You can check the state of your application at any time by printing the request, session and response system variables. One way to do it is to create a dedicated action:
2 return dict(request=request, session=session, response=response)
Was this article helpful?