beginner python cgi question

Rene Pijlman at
Sat Dec 13 11:26:12 CET 2003

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

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

René Pijlman

More information about the Python-list mailing list