6.1. User input
TheREQUEST
variable is a dictionary representing the query string if the script is
called with the HTTP GET
method, or the fields of a form submitted with the HTTP
POST
method
REQUEST
keys are the name of the fields. The values are the value of the field
- as a string,
- or a list of strings if the field name ends with
[]
(if it comes from a<SELECT MULTIPLE>
form field for instance)
Suppose you have an HTML form such as :
<form action="myScript.py"> Spam <input name="spam"> <br><select multiple name="animal[]"> <option value="dog">Dog <option value="cat">Cat <option value="frog">Frog </select> <br><input type="submit" value="Ok"> </form>
In myScript.py
the input would be displayed with :
print "<br>Spam is ",REQUEST["spam"] if REQUEST.has_key("animal"): print "<br>Animal is ",str(REQUEST["animal"])
Access to these data is available through a shortcut, consisting in the underscore _
prepended to the field name. The code above could be written in this simpler way :
print "<br>Spam is ",_spam if REQUEST.has_key("animal"): print "<br>Animal is ",str(_animal)
The underscore is introduced to reduce the risks of naming conflicts with Python reserved words or with the name of frequently used modules