[mod_python] Publisher + PSP + Sessions?

Graham Dumpleton grahamd at dscpl.com.au
Tue Aug 29 18:07:10 EDT 2006


Fredrik Sandin wrote ..
> Hello!
> 
> Given the following configuration
> 
> <Directory "...">
>      ...
>      SetHandler mod_python
>      PythonHandler mod_python.publisher
> </Directory>
> 
> and a template loading meachanism similar to the one in
> the example on the modpython homepage:
> 
> http://www.modpython.org/examples/psp_site/
> 
> how to access the session (dictionary) outside the psp
> templates?
> 
> Within a psp template it is possible to write
> 
> <%
> if not session.has_key('myvar'):
>      session['myvar'] = 0 # Default value
> %>
> 
> However, since the idea is to separate the template from
> the logic, I would prefer to do this in the python function.
> Example, inside myhandler.py:
> 
> def index(req):
>      if not X.has_key('myvar'):
>          X['myvar'] = 0 # Default value
>      return _build_page(req, template='index')
> 
> How to define X such that session['myvar'] will be defined
> inside the psp template for the index page?

If you are using mod_python 3.2.8 or later, you can say:

  def index(req):
       req.session = Session.Session(req)
       if not req.session.has_key('myvar'):
           req.session['myvar'] = 0 # Default value
       return _build_page(req, template='index')

Ie., PSP will use the session object you create and store in the request object as
req.session.

You still access the session object the same in the PSP page template. Ie., as
'session'. No need to reference it as 'req.session' in PSP page.

Graham


More information about the Mod_python mailing list