|
Robert Brewer
fumanchu at amor.org
Fri Nov 26 14:15:42 EST 2004
Josh Whiting wrote:
> 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
> # ...
> else:
> # 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
> method?
1. Better than checking strings is directly checking types:
if isinstance(fields['name'], mod_python.util.StringField):
# it's a single string value
# ...
elif isinstance(fields['name'], mod_python.util.Field):
# it's a file upload
# ...
See http://docs.python.org/lib/built-in-funcs.html
2. Here's my current method, using util.FieldStorage.list, to slurp all
of the form values at once:
data = util.FieldStorage(req, 1).list
if data is not None:
for p in data:
name = p.name
if p.filename:
filedata = p.file.read()
do_something_with_file(p.filename, filedata)
else:
do_something_with(p.value)
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
|