|
11.2.3 Higher Level InterfaceNew in version 2.2. The previous section explains how to read CGI form data using the FieldStorage class. This section describes a higher level interface which was added to this class to allow one to do it in a more readable and intuitive way. The interface doesn't make the techniques described in previous sections obsolete -- they are still useful to process file uploads efficiently, for example. The interface consists of two simple methods. Using the methods you can process form data in a generic way, without the need to worry whether only one or more values were posted under one name. In the previous section, you learned to write following code anytime you expected a user to post more than one value under one name:
This situation is common for example when a form contains a group of multiple checkboxes with the same name:
In most situations, however, there's only one form control with a particular name in a form and then you expect and need only one value associated with this name. So you write a script containing for example this code:
The problem with the code is that you should never expect that a
client will provide valid input to your scripts. For example, if a
curious user appends another "user=foo" pair to the query string,
then the script would crash, because in this situation the
Therefore, the appropriate way to read form data values was to always use the code which checks whether the obtained value is a single value or a list of values. That's annoying and leads to less readable scripts. A more convenient approach is to use the methods getfirst() and getlist() provided by this higher level interface.
Using these methods you can write nice compact code:
See About this document... for information on suggesting changes. |