Inconsistency in CGI Documentation????

Roman Suzi rnd at
Tue Aug 21 18:42:18 CEST 2001

On Tue, 21 Aug 2001, Maan M. Hamze wrote:

>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. Trying this:

>>> import os
>>> os.environ['QUERY_STRING']="aaa=1&b=&c=12"
>>> Test_Form = cgi.FieldStorage()
>>> Test_Form.keys()
['aaa', 'c']
>>> Test_Form = cgi.FieldStorage(keep_blank_values=1)
>>> Test_Form.keys()
['aaa', 'b', 'c']

I can't see any inconsitencies...
nb. the way keep_blank_values are given (it's a named argument).

>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  Slide # 40
><form method="POST" action="">
>Your first name: <input type="text" name="firstname">
><submit button goes here>
><input type="hidden" name="session" value="1f9a2">

You can

>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
>      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

Good programs are not relying on such a subtlety as default
for keepblankvalues.

>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?

Sincerely yours, Roman Suzi
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at _/
_/ Tuesday, August 21, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "If you believe in telekinesis, raise my hand." _/

More information about the Python-list mailing list