Some newbie cgi form questions...

Devan L devlai at gmail.com
Sun Aug 7 13:24:05 EDT 2005


googleboy wrote:
> for key in form.keys():Yes,  I know which fields are hidden because I
> made them that way,  but  I am trying to figure out a way I can iterate
> over the fields and do one thing with hidden fields (assign them to
> variables that tell the form how to process) and a different thing with
> non hidden fields (present them back to the browser with after
> emailling them on to the form owner).  I want this script to work fro
> several forms on the same webserver, some of which will have several
> different and individual names.  I am also hopeful that I may be able
> to extrapolate from that ways to differentiate other things from my
> form input things, but we'll see how it goes.
>
> My html code looks like this:
>
>   <h1>Send Feedback</h1>
>
>   <form action="cgi/formparse.py" method="get">
> <input name="subject" type="text" size="32" maxlength="196">Subject<br>
> <input name="name" type="text" maxlength="64">Your name<br>
> <input name="email1" type="text" size="24" maxlength="128">Your email
> address. <br>
> <br><br>Please let me know your thoughts below.<br><textarea
> name="thoughts" cols="64" rows="12"></textarea><br>
> <input  type=submit value="Submit">
> <input type="hidden" name="recipient" value="foo at bar.com">
> <input type="hidden" name="email" value="foo at bar.org">
> <input type="hidden" name="mailserver" value="localhost">
> <input name="Clear" type="button" value="Clear"></form>
>
>
> In other news, I have tried suggested syntaxes aboce (thanks guys!) but
> I get strange errors.  If the following:
>
> for key in form.keys():
>     if not form.keys():
>         print "<h1> ERROR! <h1><BR>"
>         print "Please go back and fill in all fields.  All fields are
> required.<br>"
>
> I get a traceback in teh error_log file that looks like this:
>
> Traceback (most recent call last):
>   File "cgi/form.py", line 34, in ?
>     for key, value in form.items():
>   File "/usr/local/lib/python2.3/cgi.py", line 533, in __getattr__
>     raise AttributeError, name
> AttributeError: items
>
> if this:
>
> for key in form.keys():
>     if not form.keys()[key]:
>         print "<h1> ERROR! <h1><BR>"
>         print "Please go back and fill in all fields.  All fields are
> required.<br>"
>         print '</body></html>'
>
> the I get this traceback:
>
> Traceback (most recent call last):
>   File "/var/www/users/senta/html/gobooks/cgi/form.py", line 35, in ?
>     if not form.keys()[key]:
> TypeError: list indices must be integers
>
> As you can see,  I am using python 2.3 (my web service provider is
> responsible for this - I'd use 2.4.1 if I could)
>
> TIA
>
> googleboy

>>> import cgi
>>> form = cgi.FieldStorage()
>>> dir(form)
['FieldStorageClass', '_FieldStorage__write', '__contains__',
'__doc__', '__getattr__', '__getitem__', '__init__', '__iter__',
'__len__', '__module__', '__repr__', 'bufsize', 'disposition',
'disposition_options', 'done', 'file', 'filename', 'fp', 'getfirst',
'getlist', 'getvalue', 'has_key', 'headers', 'innerboundary',
'keep_blank_values', 'keys', 'length', 'list', 'make_file', 'name',
'outerboundary', 'read_binary', 'read_lines', 'read_lines_to_eof',
'read_lines_to_outerboundary', 'read_multi', 'read_single',
'read_urlencoded', 'skip_lines', 'strict_parsing', 'type',
'type_options']

The form is not a dictionary.




More information about the Python-list mailing list