beginner python cgi question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brandon Boles

    beginner python cgi question

    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.Fiel dStorage()
    if (fields.has_key ("name") and fields.has_key( "emailaddr" )):
    SenderName=fiel ds["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
  • Rene Pijlman

    #2
    Re: beginner python cgi question

    Brandon Boles:[color=blue]
    > Address=fields["address"].value[/color]
    [...][color=blue]
    >KeyError: address[/color]
    [...][color=blue]
    > if (fields.has_key ("name") and fields.has_key( "emailaddr" )):
    > SenderName=fiel ds["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?[/color]

    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

    Comment

    • Jochen Wersdörfer

      #3
      Re: beginner python cgi question

      +--[ Brandon Boles ]---[ email@nowhere.f oo ]
      | ...snip...
      | fields=cgi.Fiel dStorage()
      | if (fields.has_key ("name") and fields.has_key( "emailaddr" )):
      | SenderName=fiel ds["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("add ress", "n/a")

      jochen

      Comment

      • Jochen Wersdoerfer

        #4
        Re: beginner python cgi question

        +--[ Brandon Boles ]---[ email@nowhere.f oo ]
        | ...snip...
        | fields=cgi.Fiel dStorage()
        | if (fields.has_key ("name") and fields.has_key( "emailaddr" )):
        | SenderName=fiel ds["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("add ress", "n/a")

        jochen

        Comment

        • Paolo G. Cantore

          #5
          Re: beginner python cgi question

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

          Hope this helps.

          Paolo

          Brandon Boles wrote:[color=blue]
          > 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.Fiel dStorage()
          > if (fields.has_key ("name") and fields.has_key( "emailaddr" )):
          > SenderName=fiel ds["name"].value
          > Address=fields["address"].value
          > City=fields["city"].value
          > State=fields["state"].value
          > ...snip...
          >[/color]

          Comment

          • Andrew Clover

            #6
            Re: beginner python cgi question

            Jochen Wersdörfer <jochen@helferl ein.net> wrote:
            [color=blue]
            > Address = fields.get("add ress", "n/a")[/color]

            (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:and@doxd esk.com

            Comment

            Working...