[mod_python] Using Templates

ron banks rebcoair at cfl.rr.com
Sat Jul 14 21:03:56 EDT 2007


Jim,

Sorry Jim, our company had a series of hardware failures taking out our
phone systems that required attention. Had to put this on hold for 24
hrs. Got to make a living while experimenting on other things, right?

Ok, it certainly is easier to debug when you get running code. I'll
claim ignorance on the dead lock in the session calls. After your tip
and seeing that in the documentation it was easy to find and correct the
offending code. The template calls are now working, thanks, and in the
long run this will be a much cleaner method for what I am doing with the
separation of code and content.

That leaves the one problem remaining with reloading or redirecting the
new page with the new date information. The function that I am using to
update the session works and yes I do a session.save() at the end. I
have a page where I can check the session values and they are updated
each time so that's not the problem. What is the best method in my
situation to use to load the page again using the new session variable
from mod_python?

On Thu, 2007-07-12 at 11:07 -0400, Jim Gallacher wrote: 
> 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:
> 
> ---------------getStrDate-------------------
> def getStrDate(req):
> 	if not hasattr(req, 'session'):
> 		req.session = Session.Session(req)
> 	if req.session.is_new():
>   		strDate = ""
>   	else:
>   		strDate = getSessionVariable(req.session, 'currDate')
>   	return strDate
> 
> 
> ----------------Month.tmpl------------------
> <%
> loadMonth(req, strDate)
> %>
> 
> 
> ----------------Month.psp------------------
> <%
> 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 
> session.save().
> 
> > 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.
> 
> Jim
> 



More information about the Mod_python mailing list