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

Gustavo Córdova Avila gustavo.cordova at q-voz.com
Mon Oct 27 10:32:52 EST 2003


Also, you can:

    author = req.form.get("author")

and if the "author" key is not present, the correct default value to 
return is None; at least that's the way it works with dictionaries.

-gustavo

Michael S. Fischer wrote:

> 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
>>
>
> _______________________________________________
> 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