| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node473.html" /> |
| 13 | <link rel="prev" href="node471.html" /> |
| 14 | <link rel="parent" href="module-cgi.html" /> |
| 15 | <link rel="next" href="node473.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>11.2.3 Higher Level Interface</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="11.2.2 Using the cgi" |
| 25 | href="node471.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="11.2 cgi " |
| 28 | href="module-cgi.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="11.2.4 Old classes" |
| 31 | href="node473.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node471.html">11.2.2 Using the cgi</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-cgi.html">11.2 cgi </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node473.html">11.2.4 Old classes</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0013230000000000000000"> |
| 56 | 11.2.3 Higher Level Interface</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | |
| 61 | <span class="versionnote">New in version 2.2.</span> |
| 62 | |
| 63 | <P> |
| 64 | The previous section explains how to read CGI form data using the |
| 65 | <tt class="class">FieldStorage</tt> class. This section describes a higher level |
| 66 | interface which was added to this class to allow one to do it in a |
| 67 | more readable and intuitive way. The interface doesn't make the |
| 68 | techniques described in previous sections obsolete -- they are still |
| 69 | useful to process file uploads efficiently, for example. |
| 70 | |
| 71 | <P> |
| 72 | The interface consists of two simple methods. Using the methods |
| 73 | you can process form data in a generic way, without the need to worry |
| 74 | whether only one or more values were posted under one name. |
| 75 | |
| 76 | <P> |
| 77 | In the previous section, you learned to write following code anytime |
| 78 | you expected a user to post more than one value under one name: |
| 79 | |
| 80 | <P> |
| 81 | <div class="verbatim"><pre> |
| 82 | item = form.getvalue("item") |
| 83 | if isinstance(item, list): |
| 84 | # The user is requesting more than one item. |
| 85 | else: |
| 86 | # The user is requesting only one item. |
| 87 | </pre></div> |
| 88 | |
| 89 | <P> |
| 90 | This situation is common for example when a form contains a group of |
| 91 | multiple checkboxes with the same name: |
| 92 | |
| 93 | <P> |
| 94 | <div class="verbatim"><pre> |
| 95 | <input type="checkbox" name="item" value="1" /> |
| 96 | <input type="checkbox" name="item" value="2" /> |
| 97 | </pre></div> |
| 98 | |
| 99 | <P> |
| 100 | In most situations, however, there's only one form control with a |
| 101 | particular name in a form and then you expect and need only one value |
| 102 | associated with this name. So you write a script containing for |
| 103 | example this code: |
| 104 | |
| 105 | <P> |
| 106 | <div class="verbatim"><pre> |
| 107 | user = form.getvalue("user").upper() |
| 108 | </pre></div> |
| 109 | |
| 110 | <P> |
| 111 | The problem with the code is that you should never expect that a |
| 112 | client will provide valid input to your scripts. For example, if a |
| 113 | curious user appends another "<tt class="samp">user=foo</tt>" pair to the query string, |
| 114 | then the script would crash, because in this situation the |
| 115 | <code>getvalue("user")</code> method call returns a list instead of a |
| 116 | string. Calling the <tt class="method">toupper()</tt> method on a list is not valid |
| 117 | (since lists do not have a method of this name) and results in an |
| 118 | <tt class="exception">AttributeError</tt> exception. |
| 119 | |
| 120 | <P> |
| 121 | Therefore, the appropriate way to read form data values was to always |
| 122 | use the code which checks whether the obtained value is a single value |
| 123 | or a list of values. That's annoying and leads to less readable |
| 124 | scripts. |
| 125 | |
| 126 | <P> |
| 127 | A more convenient approach is to use the methods <tt class="method">getfirst()</tt> |
| 128 | and <tt class="method">getlist()</tt> provided by this higher level interface. |
| 129 | |
| 130 | <P> |
| 131 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 132 | <td><nobr><b><tt id='l2h-3159' xml:id='l2h-3159' class="method">getfirst</tt></b>(</nobr></td> |
| 133 | <td><var>name</var><big>[</big><var>, default</var><big>]</big><var></var>)</td></tr></table></dt> |
| 134 | <dd> |
| 135 | This method always returns only one value associated with form field |
| 136 | <var>name</var>. The method returns only the first value in case that |
| 137 | more values were posted under such name. Please note that the order |
| 138 | in which the values are received may vary from browser to browser |
| 139 | and should not be counted on.<A NAME="tex2html53" |
| 140 | HREF="#foot47232"><SUP>11.1</SUP></A> If no such form |
| 141 | field or value exists then the method returns the value specified by |
| 142 | the optional parameter <var>default</var>. This parameter defaults to |
| 143 | <code>None</code> if not specified. |
| 144 | </dl> |
| 145 | |
| 146 | <P> |
| 147 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 148 | <td><nobr><b><tt id='l2h-3160' xml:id='l2h-3160' class="method">getlist</tt></b>(</nobr></td> |
| 149 | <td><var>name</var>)</td></tr></table></dt> |
| 150 | <dd> |
| 151 | This method always returns a list of values associated with form |
| 152 | field <var>name</var>. The method returns an empty list if no such form |
| 153 | field or value exists for <var>name</var>. It returns a list consisting |
| 154 | of one item if only one such value exists. |
| 155 | </dl> |
| 156 | |
| 157 | <P> |
| 158 | Using these methods you can write nice compact code: |
| 159 | |
| 160 | <P> |
| 161 | <div class="verbatim"><pre> |
| 162 | import cgi |
| 163 | form = cgi.FieldStorage() |
| 164 | user = form.getfirst("user", "").upper() # This way it's safe. |
| 165 | for item in form.getlist("item"): |
| 166 | do_something(item) |
| 167 | </pre></div> |
| 168 | |
| 169 | <P> |
| 170 | <BR><HR><H4>Footnotes</H4> |
| 171 | <DL> |
| 172 | <DT><A NAME="foot47232">... on.</A><A |
| 173 | HREF="node472.html#tex2html53"><SUP>11.1</SUP></A></DT> |
| 174 | <DD>Note that some recent |
| 175 | versions of the HTML specification do state what order the |
| 176 | field values should be supplied in, but knowing whether a |
| 177 | request was received from a conforming browser, or even from a |
| 178 | browser at all, is tedious and error-prone. |
| 179 | |
| 180 | </DD> |
| 181 | </DL> |
| 182 | <DIV CLASS="navigation"> |
| 183 | <div class='online-navigation'> |
| 184 | <p></p><hr /> |
| 185 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 186 | <tr> |
| 187 | <td class='online-navigation'><a rel="prev" title="11.2.2 Using the cgi" |
| 188 | href="node471.html"><img src='../icons/previous.png' |
| 189 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 190 | <td class='online-navigation'><a rel="parent" title="11.2 cgi " |
| 191 | href="module-cgi.html"><img src='../icons/up.png' |
| 192 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 193 | <td class='online-navigation'><a rel="next" title="11.2.4 Old classes" |
| 194 | href="node473.html"><img src='../icons/next.png' |
| 195 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 196 | <td align="center" width="100%">Python Library Reference</td> |
| 197 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 198 | href="contents.html"><img src='../icons/contents.png' |
| 199 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 200 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 201 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 202 | <td class='online-navigation'><a rel="index" title="Index" |
| 203 | href="genindex.html"><img src='../icons/index.png' |
| 204 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 205 | </tr></table> |
| 206 | <div class='online-navigation'> |
| 207 | <b class="navlabel">Previous:</b> |
| 208 | <a class="sectref" rel="prev" href="node471.html">11.2.2 Using the cgi</A> |
| 209 | <b class="navlabel">Up:</b> |
| 210 | <a class="sectref" rel="parent" href="module-cgi.html">11.2 cgi </A> |
| 211 | <b class="navlabel">Next:</b> |
| 212 | <a class="sectref" rel="next" href="node473.html">11.2.4 Old classes</A> |
| 213 | </div> |
| 214 | </div> |
| 215 | <hr /> |
| 216 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 217 | </DIV> |
| 218 | <!--End of Navigation Panel--> |
| 219 | <ADDRESS> |
| 220 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 221 | </ADDRESS> |
| 222 | </BODY> |
| 223 | </HTML> |