[mod_python] Using session management?

Jim Gallacher jg.lists at sympatico.ca
Thu Sep 15 21:45:30 EDT 2005

Note that when you correct your code you'll likely still raise an 
exception since you have not correctly saved the data in the dictionary. 
  A valid session object will exist but it will not have the 'myVar2' 
key. Either clear the pysid cookie in your browser or modify your code 
to catch the exception.

Also, calling sess.load() is redundant. It is called automatically by 
the Session.Session constructor if there is valid session data in the 
persistent store.


Jason Stubbs wrote:
> Steve Bergman wrote:
>> In my previous message I cut and pasted the wrong snippet.  This is 
>> what I meant:
>> from mod_python import Session
>> def main(req):
>>       sess = Session.Session(req)
>>       if sess.is_new():
>>       s = '<html>This is a new session.</html>'
>>       sess.myVar2 = "I am a session variable."
>>       sess.save()
>>       return s
>>   else:
>>       sess.load()
>>       s='<html>This is an old session.  myVar2 = ' + sess.myVar2 + 
>> '</html>'
>>       sess.save()
>>       return s
> "BaseSession is a subclass of dict. Data can be stored and retrieved 
> from the session by using it as a dictionary." -- docs
> It looks like what you want is:
> -       sess.myVar2 = "I am a session variable."
> +       sess["myVar2"] = "I am a session variable."
> -- 

More information about the Mod_python mailing list