[mod_python] Beginner question re: getting form data and key errors when var not defined

Michael S. Fischer michael at dynamine.net
Sat Oct 25 16:32:29 EST 2003


This is a really a Python language question, not a mod_python question 
per se.

You have several options, including:

if req.form.has_key('author'):
   author = req.form['author']
else:
   author = None

Or:

try:
   author = req.form['author']
except KeyError:
   author = None

See section 2.2.7 ("Mapping Types") in the Python Library Reference for 
details.

--Michael

Mike Klein wrote:

> Why am I getting key errors when merely attempting to get <possible> 
> form data from a mod_python request...ala:
> 
> author = req.form['author']
> 
> I get the following:
> 
>    raise KeyError, key
> 
> KeyError: subject
> 
> I do kind of expect certain form fields to possibly not have 
> values...why the explicit error? Can't I just check for null or 
> uninitialized in my code?
> 
> thanks,
> 
> mike
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



More information about the Mod_python mailing list