4.6.1 FieldStorage class

Access to form data is provided via the FieldStorage class. This class is similar to the standard library module cgi FieldStorage.

class FieldStorage(req[, keep_blank_values, strict_parsing])
This class provides uniform access to HTML form data submitted by the client. req is an instance of the mod_python request object.

The optional argument keep_blank_values is a flag indicating whether blank values in URL encoded form data should be treated as blank strings. The default is false, which means that blank values are ignored as if they were not included.

The optional argument strict_parsing is not yet implemented.

During initialization, FieldStorage class reads all of the data provided by the client. Since all data provided by the client is consumed at this point, there should be no more than one FieldStorage class instantiated per signle request, nor should you make any attempts to read client data before or after instantiating a FieldStorage.

The data read from the client is then parsed into separate fields and packaged in Field objects, one per field. For HTML form inputs of type file, a temporary file is created that can later be accessed via the file attribute of a Field object.

The FieldStorage class has a mapping object interface, i.e. it can be treated like a dictionary. When used as a mapping, the keys are form input names, and the returned dictionary value can be:

  • 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.
  • An instances of a Field class, if the input is a file upload.
  • A list of StringField and/or Field objects. This is when multiple values exist, such as for a <select> HTML form element.

Note that unlike the standard library cgi module FieldStorage class, a Field object is returned only when it is a file upload. In all other cases an instance the return is an instance of StringField, which is a subclass of str. This means that you do not need to use the .value attribute to access values of fields in most cases.

In addition to standard mapping object methods, FieldStorage objects have the following attributes:

list
This is a list of Field objects, one for each input. Multiple inputs with the same name will have multiple elements in this list.

What is this????