[mod_python] Session Management for the beginner

Jim Steil jim at qlf.com
Thu Jan 5 01:06:30 EST 2006


Ok, now I get it, and have it working.  I'm pretty sure my problem was 
that I was trying to seed the session with some values in my 
authentication handler and then grab the values when it got to my 
secUserList handler.  I took the stuff out of my authentication handler 
and now it works fine.  But, I'm interested in pursuing something you 
wrote in your earlier post saying that I could pass the session object 
in the req object.  I may give that a try tomorrow.  Otherwise, I'll 
assuming I could set my values in a cookie and read them later.

Thanks for the help, and especially for sticking with me until I 
understood what was going on.  Things are much clearer now for me.

    -Jim

Jim Steil
IT Manager
Quality Liquid Feeds
(608) 935-2345


Jim Gallacher wrote:
> Jim Steil wrote:
>> Jim:
>>
>> Thanks for the info.  However, I'm still having trouble.  I don't 
>> know if this is the problem, but this code does NOT exist in a 
>> handler that I wrote.  I didn't write a handler, I'm just using the 
>> publisher handler.  I have written an authentication handler and that 
>> is working correctly but I don't know what I need to do to make my 
>> own handler and have it work like the publisher handler.  Again, I'm 
>> probably trying too hard and not seeing the obvious.  I have been 
>> looking all over the web and can't seem to find a good custom handler 
>> example.
>>
>> Thanks again, I'll keep plugging away to try to understand what's 
>> going on.
>>
>>    -Jim
>
>
> I tend to use the term "handler" to refer to anything that handles a 
> request, which is just plain wrong. ;) If you want to use the 
> publisher handler just drop a script in the appropriate directory and 
> either call it (which will used the index method by default) or one of 
> it's other methods.
>
> Plop the following in your motion folder and try calling it with 
> http://example.com/motion/mptest or 
> http://example.com/motion/mptest/secUserList. (Note that the 
> indentation may have been messed up when I did the copy and paste from 
> your original code). Also note that I forgot the req parameter in my 
> previous Session example. :(
>
> You can modify your secUserList.html to make use of the count 
> paramater passed to your psp template and see the effect of the session.
>
> Jim
>
> mptest.py
> ---------
>
> # uses the mod_python.publisher handler
>
> import time
> import mx.ODBC
> import mx.ODBC.Windows
>
> from mod_python import Session
>
>
> # url is http://example.com/motion/mptest
> def index(req):
>     sess = Session.Session(req)
>     if sess.is_new():
>         sess['count'] = 0
>     sess['count'] += 1
>     req.content_type = 'text/plain'
>     req.write('This is the index page\nVisits: %d\n' % sess['count'])
>     sess.save()
>
>
> # url is http://example.com/motion/mptest/secUserList
> def secUserList(req, page='', sortSeq=''):
>     sess = Session.Session(req)
>     if sess.is_new():
>         sess['count'] = 0
>     sess['count'] += 1
>     sess.save()
>
>     if page:
>         startPage = int(page)
>     else:
>         startPage = 1
>     pageLength = 20
>     sql = 'call secUser_list(\'' + sortSeq + '\', ' + repr(startPage) 
> + ', ' + repr(pageLength) + ')'
>     db = 
> mx.ODBC.Windows.DriverConnect('DSN=motion;UID=userid;PWD=password')
>     c = db.cursor()
>     c.execute(sql)
>     htmlText = ''
>     for secUserId, firstName, lastName, logonName, email in c.fetchall():
>         htmlText = htmlText + '<tr 
> onmouseover="style.backgroundColor=\'#FFF0AF\'; 
> style.cursor=\'hand\';" 
> onclick="location.href=\'secUserProfile?secUserId=' + "%i" % secUserId 
> + '\'" onmouseout="style.backgroundColor=\'#ffffff\';"><td>' + 
> firstName + '</td><td>' + lastName + '</td><td>' + logonName + 
> '</td><td>' + email + '</td></tr>\n'
>         previousPage = 1
>     if startPage > 1:
>         previousPage = startPage - 1
>         nextPage = startPage + 1
>     c.close()
>     db.close()
>     parmDict = {'count':sess['count'], 'title':'User List', 
> 'rowHtml':htmlText, 'previousPage':previousPage, 'nextPage':nextPage, 
> 'startPage':startPage}
>     template = psp.PSP(req, filename='secUserList.html')
>     template.run(vars = parmDict)
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20060105/e4509ad6/attachment.html


More information about the Mod_python mailing list