Castalian
The Request Object
The Request object is used to get details that the browser and user supply.
request.querystring dictionary
foo = request.querystring["var"]
The query string dictionary contains all variables supplied in the URL;
for example, a page called as http://www.foo.com/index.cas?id=2&name=wibble
would have request.querystring set to the Python dictionary
{ "id": "2", "name": "wibble" }.QueryString notes:
- If the same variable is specified twice then the dictionary value for that variable will be a list, not a single value.
- Unlike ASP, variables that are not specified in the querystring are not
set to blank.
request.querystring["foo"]without a "foo" variable in the querystring will generate a KeyError. Therefore, instead of the ASP idiom:
use a more Pythonicif Request.QueryString("foo") = "" then foo = Request.QueryString("foo") ... do something with foo ... end if
Note the difference; has_key is a function, and uses ordinary brackets (), while actually retrieving the value from the querystring is a dictionary retrieval and hence uses square brackets [].if request.querystring.has_key("foo"): foo = request.querystring["foo"] ... do something with foo ... - Because it's quite common to want to check whether a number of fields
exist in the querystring (after form submissions, where you want to act if the
user filled in all essential elements of the form), the querystring dictionary
supports a has_keys() function. This works like has_key(), except that you may
specify more than one key as a parameter and it returns true if all keys are in
the dictionary.
request.form dictionary
foo = request.form["var"]Therequest.formdictionary works exactly like therequest.querystringdictionary, but contains values from forms that were POSTed to the page. See above for details.request.servervariables dictionary
This dictionary contains all variables in the webserver's environment.request.cookies dictionary
All cookies that are supplied by the browser to this page live in this dictionary.