[mod_python] mod_python newbie help

Jorey Bump list at joreybump.com
Mon Oct 3 17:40:11 EDT 2005


Graham Dumpleton wrote:
> 
> On 04/10/2005, at 4:18 AM, Jorey Bump wrote:
 >
>> and/or use conventional Python exceptions to handle errors:
>>
>> try:
>>     a = req.form['myvar']
>> except KeyError, key:
>>     return '<h1>No form variable by that name: %s</h1>' % (key,)
> 
> This unfortunately doesn't help with mod_python.publisher unless you are
> prepared to throw away its abilities to automatically decode form
> parameters into function arguments.

I am prepared to do so, and have for a long time. :) Nonetheless, I'm 
not sure why you think this is unhelpful with Publisher. As long as you 
take req and the automatically decoded parameter in your function, you 
can refer to them both ways:

def compare(req, myvar):
     if req.form['myvar'] == myvar:
         return 'they are the same'
     else:
         # this should never happen
         return 'they are not the same'

> The closest you will get if you want to preserve this automatic form
> argument decoding... 

My advice to Publisher newbies is to use this feature sparingly. It 
encourages butt-ugly code and just feels unsafe (although I can't 
justify the latter with an example).

To illustrate, compare the following:

def index(myvar1, myvar2, myvar3, myvar4, myvar5)
     return processform(myvar1, myvar2, myvar3, myvar4, myvar5)

with:

def index(req)
     return processform(req)

I think the last example is much cleaner. To get the most out of 
mod_python and Publisher, it's worthwhile to learn as much about req as 
possible. Unfortunately, there's no single page in the documentation 
that shows all possible members/methods belonging to the req object 
(especially in the Publisher handler), so I can't provide a link.





More information about the Mod_python mailing list