[mod_python] determining type of fieldStorage dictionary value?

Josh Whiting josh-modpython at valleyfree.com
Fri Nov 26 13:33:48 EST 2004

according to the mod_python documentation section 4.6.1, fieldStorage is 
a dictionary-like object in which keys are the form input names and the 
values can be:

1. An instance of StringField, containing the form input value. This is 
only when there is a single value corresponding to the input name. 
StringField is a subclass of str which provides the additional value 
attribute for compatibility with standard library cgi module.

2. An instances of a Field class, if the input is a file upload.

3. A list of StringField and/or Field objects. This is when multiple 
values exist, such as for a <select> HTML form element.

So how do I tell which one it is?  Even if I design my html to work one
way, I can't assume the input request will conform to that. Definately a
noob question but I couldn't find an answer in the python docs, I 
assume it is in there somewhere...  by trial and error I figured I could 
do this:

fields = util.fieldStorage(req)
if str(type(fields['name'])) == "<class 'mod_python.util.StringField'>":
    # it's a single string value
    # ...
elif str(type(fields['name'])) == "<class 'mod_python.util.Field'>":
    # it's a file upload
    # ...
    # it's a list of stringField objects
    # ...

however, this seemed somehow incorrect to compare the output of 
str(type(...)) to a known string value... or this is the preferred 

/josh w

More information about the Mod_python mailing list