CGI causes me some headaches..
Michael P. Reilly
arcege at shore.net
Thu May 13 17:42:18 EDT 1999
Tim B <tim at onegoodidea.com> wrote:
: I have a simple HTML form which has a text field,(textfield) a select box
: (select)
: and a submit command button (Submit).
: the submit button causes the following script to be run using the
: post method of the form.
: #! /usr/bin/python1.5
: import cgi
: print "Content-type: text/html"
: print
: print "<TITLE>CGI script output</TITLE>"
: print "<H1>This is my first CGI script</H1>"
: cgi.test()
: form = cgi.FieldStorage
: submit = form(0)
: print submit
: Ignoring all of the "cgi.test" output, the "print submit" line gives rise
: to:
: FieldStorage(None, None, [])
: So I tried using the GET method and got the following:
: FieldStorage(None, None, [MiniFieldStorage('Submit', 'Submit'),
: MiniFieldStorage('textfield', 'Ritchie Hawtin'), MiniFieldStorage('select',
: 'techno')])
: This is more promising as I can see the values that were originally entered
: into the form.
: I am wondering how I would go about actually extracting the values ("Ritchie
: Hawtin","techno") from the structure.
: If I try:
: print submit (2)
: I get the following error:
: Traceback (innermost last): File "/home/httpd/cgi-bin/RP-cgi", line 13, in ?
: print submit (2) AttributeError: no __call__ method defined
You might want to look at the module documentation:
http://www.python.org/doc/current/lib/module-cgi.html
or in the module file itself:
/usr/.../lib/python1.5/cgi.py
The form that is returned from calling the FieldStorage() class is very
similar to a dictionary. The contents of each dictionary item is an
object with a "value" attribute; if you had more than one <INPUT
name=foo> tag in the form, then the contents will be a list of such
items.
form = cgi.FieldStorage()
if form.has_key('textfield'):
print 'textfield = ', repr(form['textfield'].value)
else:
print 'nothing was entered for textfield'
if form.has_key('select'):
print 'the option selected is', form['select'].value
else:
print 'no option was selected'
Only call FieldStorage once, it will process standard input (for POST
requests) or the environment variables (for GETs) as needed. Calling
it twice on POST requests might get the system confused since standard
input had already been read.
-Arcege
PS: it is unlikely that a "Submit" field will exist in the form unless
you had a "name=" attribute in the <input type=submit> tag.
More information about the Python-list
mailing list