beginner python cgi question

B

Brandon Boles

Hi folks,

I am trying to learn python, and have a form processing script giving me
this error:

Traceback (most recent call last):
File "inquiry", line 16, in ?
Address=fields["address"].value
File "/usr/local/python2.2/lib/python2.2/cgi.py", line 550, in __getitem__
raise KeyError, key
KeyError: address

This is the snippet of code the error is referring to:

....snip...
fields=cgi.FieldStorage()
if (fields.has_key("name") and fields.has_key("emailaddr")):
SenderName=fields["name"].value
Address=fields["address"].value
City=fields["city"].value
State=fields["state"].value
....snip...

I think what the problem is that if my form does not have one of the
fields (except 'name' and 'emailaddr') with data in it, I get this error.
What is the best way to fix this?

Please be kind, this is my first attempt at python. Thanks for the help!

Brandon
 
R

Rene Pijlman

Brandon Boles:
Address=fields["address"].value [...]
KeyError: address [...]
if (fields.has_key("name") and fields.has_key("emailaddr")):
SenderName=fields["name"].value
Address=fields["address"].value
City=fields["city"].value
State=fields["state"].value
...snip...

I think what the problem is that if my form does not have one of the
fields (except 'name' and 'emailaddr') with data in it, I get this error.
What is the best way to fix this?

One way is to use if has_key(), as you already discovered. The other way
is exception handling:

try:
Address = fields["address"].value
except KeyError, e:
Address = None # and/or report an error
else:
# do something with Address
 
J

Jochen =?iso-8859-1?Q?Wersd=F6rfer?=

+--[ Brandon Boles ]---[ (e-mail address removed) ]
| ...snip...
| fields=cgi.FieldStorage()
| if (fields.has_key("name") and fields.has_key("emailaddr")):
| SenderName=fields["name"].value
| Address=fields["address"].value
| City=fields["city"].value
| State=fields["state"].value
| ...snip...
|
| I think what the problem is that if my form does not have one of the
| fields (except 'name' and 'emailaddr') with data in it, I get this error.
| What is the best way to fix this?

Getting a default value, if "address" is not set:

Address = fields.get("address", "n/a")

jochen
 
J

Jochen Wersdoerfer

+--[ Brandon Boles ]---[ (e-mail address removed) ]
| ...snip...
| fields=cgi.FieldStorage()
| if (fields.has_key("name") and fields.has_key("emailaddr")):
| SenderName=fields["name"].value
| Address=fields["address"].value
| City=fields["city"].value
| State=fields["state"].value
| ...snip...
|
| I think what the problem is that if my form does not have one of the
| fields (except 'name' and 'emailaddr') with data in it, I get this error.
| What is the best way to fix this?

Getting a default value, if "address" is not set:

Address = fields.get("address", "n/a")

jochen
 
P

Paolo G. Cantore

Use 'fields=cgi.FieldStorage(keep-blank-values=1)' and you don't have to
worry about KeyErrors anymore.

Hope this helps.

Paolo
 
A

Andrew Clover

Jochen Wersdörfer said:
Address = fields.get("address", "n/a")

(You mean fields.getvalue, natch.)

If you don't want to have to deal with cgi returning a list sometimes, what
you really want is fields.getfirst('fieldname', 'default'), but this only
exists in Python 2.2 onwards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top