[mod_python] Sessions again...

Jim Gallacher jpg at jgassociates.ca
Thu Jan 19 17:25:12 EST 2006


Luis M. Gonzalez wrote:
> Hi folks,
> 
> I made significant progress since I started playing with mod_python last week-end,
> and I created a shopping cart and a script for handling pagination of recorsets with record navigation bars.
> Everything works great, except for one little problem:
> 
> If I test one of these scripts (any of them), it works great, but if I test the other one without restarting the browser, I get a strange error message that, I guess, has to do with session variables.
> This is the error message:
> 
> Mod_python error: "PythonHandler mod_python.psp"
> 
> Traceback (most recent call last):
> 
>   File "C:\Python24\Lib\site-packages\mod_python\apache.py", line 299, in HandlerDispatch
>     result = object(req)
> 
>   File "C:\Python24\Lib\site-packages\mod_python\psp.py", line 302, in handler
>     p.run()
> 
>   File "C:\Python24\Lib\site-packages\mod_python\psp.py", line 213, in run
>     exec code in global_scope
> 
>   File "C:/Archivos de programa/Apache Group/Apache2/htdocs/test/zzxx.psp", line 18, in ?
>     tmpl.run(vars = {   'dbmodule': dbmodule,
> 
>   File "C:\Python24\Lib\site-packages\mod_python\psp.py", line 213, in run
>     exec code in global_scope
> 
>   File "C:/Archivos de programa/Apache Group/Apache2/htdocs/test\recset.tmpl", line 111, in ?
>     r.counter = s['counter']
> 
> KeyError: 'counter'
> Here, "counter" is a session variable I defined in one of these scripts.
> If I test the other script first and then this one, I get the same error message, but instead of pinting to "counter", it points to "items", which is the other session variable.
> 
> Summing up:
> Each script works great when tested first, but the second one gives me this error message.
> On the other hand, If I test them separately (restarting the browser for each one), they both work great.
> 
> What could be the problem?
> Any hint would be highy appreciated...

You'll need to clarify your problem a little, but I think you may be 
assuming that you get a different session object for each script. That 
is not the case, at least by default. You get one session object per 
browser so if you want to use it in independent scripts, each script 
will need to catch the KeyError exception or do some other test to see 
if you need to initialize your session data.

You can also do some thing like this:

r.counter = sess.get('counter', 0)
...
...
r.counter += 1
sess['counter'] = r.counter
sess.save()

Jim


More information about the Mod_python mailing list