Inconsistency in CGI Documentation????

Maan M. Hamze mmhamze at pleiades.net
Tue Aug 21 17:16:49 CEST 2001


While working on a Python CGI script I ran into the following:
Assume I have a form with a Text Input element.  The element has a name, but
I chose not to set its value, or to set value to "" (empty).
I import CGI module and define:  Test_Form = cgi.FieldStorage()
1.  PythonWin documentation of Python states the following:
Form fields containing empty strings are ignored and do not appear in the
dictionary; to keep such values, provide the optional "keep_blank_values"
argument when creating the FieldStorage instance.
Question:  How do you provide this argument?
Test_Form=cgi.FieldStorage(keep_blank_values)??  This is not working.

Ok!  I am not going to provide this argument.  So, the Text Input element
that I left empty and had not provided a vlaue for should NOT make it into
FieldStorage() if user does not type some characters in it.  Right?  I tried
it.  It does not make it.
Even if I provide a value of " " (is " " an empty string??)

2.  BUT, read on:
check http://www.python.org/doc/essays/ppt/sd99east/sld041.htm  Slide # 40
<form method="POST" action="http://host.com/cgi-bin/test.py">
Your first name: <input type="text" name="firstname">
<submit button goes here>
<input type="hidden" name="session" value="1f9a2">
</form>

Ok!  So here it is, a Text Input element with no value.  From the
documentation above in 1., this element should not be in FieldStorage unless
one types in something.
Now, move into Slide 41:
 print "Content-type: text/html\n"
 form = cgi.FieldStorage()     # parse query
 if form.has_key("firstname")  and  form["firstname"].value != "":
      print "Hello", form["firstname"].value
else:
      print "Error! Please enter first name."

Of interest is:   if form.has_key("firstname")  and  form["firstname"].value
!= ""
How come we are checking for the key AND that the vlaue of the key is not
""?  Is not that code assuming that a non-empty string can be included in
FieldStorage() without using keep_blank_values?  Thus a contradiction with
what point 1. has to say about it?

I am really trying to understand this.  Can someone help?
Thanks,
Maan





More information about the Python-list mailing list