[mod_python] Using Templates

Jim Gallacher jpg at jgassociates.ca
Thu Jul 12 11:07:37 EDT 2007

ron banks wrote:
> Graham,
> Thanks for the reply. I do realize that the '=' is wrong. When removed
> the browser just hangs without reporting errors.

It occurs to me that you may have a session deadlock. Do you use the 
"session" variable name anywhere in any of your psp templates, or is it 
just in setStrDate?

The rule is that you should only create one session instance per 
request. When a session instance is created it is locked for the 
duration of the request to protect your data. If you attempt to create a 
second instance the request will appear to hang as the 2nd session waits 
for the lock to be released, which of course never happens.

There is some "magic" in psp where it will create a session instance if 
it finds a variable named "session" anywhere in the template. It will 
check to see if it can find req.session first, so if you need to create 
a session outside of psp make sure you assign it to req.session. This is 
probably a good habit to get into even if you aren't using the psp handler.

> What I am not getting
> is how to properly set the 'strDate' variable in:

Actually, I think you are setting it properly. ;)

> ----------------Month.tmpl------------------
> <%=loadMonth(req, strDate)%>
>                   ^^^^^^^
> with this function:
> ---------------setStrDate-------------------
> def setStrDate(req):
> 	session = Session.Session(req)
> 	if session.is_new():
> 		strDate = ""
> 	else:
> 		strDate = getSessionVariable(session, 'currDate')
> 	req.content_type = 'text/html'
> 	tmpl = psp.PSP(req, filename='Month.tmpl')
> 	tmpl.run(vars = {'strDate': strDate})
> 	return ""
> called from:
> ----------------Month.psp------------------
> <%
> from mod_python import apache, psp
> from loadCalendar import setStrDate
> %>
> <%=setStrDate(req)%>
> How should this be done? This is a first experiment for me utilizing
> templates. What I am trying to do is to set a session variable
> 'currDate' with a new date through ajax methods and load a page with the
> same loadMonth function which builds a calendar populated with records
> from a database backend. I have worked out almost everything else and
> have only a couple of problems before I can move forward.

You're on the right track. I would suggest that you simplify 
setStrDate() so that it only does one thing. You might also consider 
renaming it to getStrDate as that seems to be it's function. Move the 
things not related to the date to another function. It'll make your 
program logic easier to understand. It might not see like a big deal 
right now, but if need to come back and modify the code 6 months from 
now you'll thank me. ;)

Your code might look more like this:

def getStrDate(req):
	if not hasattr(req, 'session'):
		req.session = Session.Session(req)
	if req.session.is_new():
  		strDate = ""
  		strDate = getSessionVariable(req.session, 'currDate')
  	return strDate

loadMonth(req, strDate)

from mod_python import apache, psp
from loadCalendar import getStrDate

strDate = getStrDate(req)

tmpl = psp.PSP(req, filename='Month.tmpl')
tmpl.run(vars = {'strDate': strDate})


> I either need to figure out how to utilize a template system as
> described which I think might be preferable in this situation or
> possibly reload the same page letting the loadMonth function pick up the
> changed session variable and building the page based on the new date.
> I'm not sure how to reload the page. Should I use some type of redirect
> from the server side or javascript from the client side? I have only
> started to experiment with these methods. I can't seem to get the
> redirect to do anything yet.  With the javascript reload the session
> variable seems to go away.

This may be related to the way you are using sessions. Once we've 
determined if you have a deadlock we can deal with this issue.

Do you save the session anywhere, or are you depending on psp? Psp will 
only save the session if it finds the "session" variable name in your 
template. Otherwise you'll need to save it explicitly somewhere with 

> Is this because of the new request or
> something? Maybe a better method is to control session information in
> the database and just use javascript. I don't know what differences in
> speed would be. What do you think?

Use the mod_python session machinery until you've got it working. Adding 
another layer of complexity won't help at this point.

As I mull this over I'm convinced you're problem lies with the way you 
are using sessions, and not with psp templates.


More information about the Mod_python mailing list