beginner python cgi question

Discussion in 'Python' started by Brandon Boles, Dec 13, 2003.

  1. 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
     
    Brandon Boles, Dec 13, 2003
    #1
    1. Advertising

  2. Brandon Boles

    Rene Pijlman Guest

    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

    --
    René Pijlman
     
    Rene Pijlman, Dec 13, 2003
    #2
    1. Advertising

  3. +--[ Brandon Boles ]---[ ]
    | ...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
     
    Jochen =?iso-8859-1?Q?Wersd=F6rfer?=, Dec 13, 2003
    #3
  4. +--[ Brandon Boles ]---[ ]
    | ...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
     
    Jochen Wersdoerfer, Dec 13, 2003
    #4
  5. Use 'fields=cgi.FieldStorage(keep-blank-values=1)' and you don't have to
    worry about KeyErrors anymore.

    Hope this helps.

    Paolo

    Brandon Boles wrote:
    > 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...
    >
     
    Paolo G. Cantore, Dec 13, 2003
    #5
  6. Jochen Wersdörfer <> wrote:

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

    --
    Andrew Clover
    mailto:
    http://www.doxdesk.com/
     
    Andrew Clover, Dec 14, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    653
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    1,041
    Mabden
    Sep 2, 2004
  3. LarsenMTL
    Replies:
    4
    Views:
    854
    Eric Walstad
    Nov 4, 2004
  4. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    261
    7stud --
    Apr 12, 2011
  5. John

    forms, cgi - beginner question

    John, Feb 15, 2005, in forum: Perl Misc
    Replies:
    15
    Views:
    834
Loading...

Share This Page