Handling empty form fields in CGI

Discussion in 'Python' started by Christopher Mocock, Jan 26, 2007.

  1. Hi all,

    Bit of a python newbie so need a little help with a CGI script I'm
    trying to write. I've got it working fine as long as the fields of the
    form are filled in correctly, however I need to be able to accept blank
    entries. Therefore I want to convert any empty entries to an empty string.

    For example, if I call the following CGI script as:

    http://localhost/cgi-bin/test.cgi?myfield=hello

    ....it prints hello in the browser. But if I call it as:

    http://localhost/cgi-bin/test.cgi?myfield=

    I get an exception. I want it to treat myfield as an empty string and
    not throw an exception.

    Any suggestions?

    Thanks in advance, Chris.

    Script below:


    #!/usr/bin/env python2
    # Import CGI and CGI debug libraries
    import cgi
    import cgitb; cgitb.enable()

    def test():
    value=form["myfield"].value
    print value

    # Start of main code
    print 'Content-type: text/html'
    print

    # Get the contents of the query string
    form = cgi.FieldStorage()

    test()
    Christopher Mocock, Jan 26, 2007
    #1
    1. Advertising

  2. Christopher Mocock

    Peter Otten Guest

    Christopher Mocock wrote:

    > Bit of a python newbie so need a little help with a CGI script I'm
    > trying to write. I've got it working fine as long as the fields of the
    > form are filled in correctly, however I need to be able to accept blank
    > entries. Therefore I want to convert any empty entries to an empty string.
    >
    > For example, if I call the following CGI script as:
    >
    > http://localhost/cgi-bin/test.cgi?myfield=hello
    >
    > ...it prints hello in the browser. But if I call it as:
    >
    > http://localhost/cgi-bin/test.cgi?myfield=
    >
    > I get an exception. I want it to treat myfield as an empty string and
    > not throw an exception.
    >
    > Any suggestions?


    You can catch the exception:

    def test():
    try:
    value = form["myfield"].value
    except KeyError:
    value = ""
    print value

    or use form.getlist("myfield") and then check the length of the returned
    list of values.

    With both options you also have to decide what to do when 'myfield' is given
    twice:

    http://localhost/cgi-bin/test.cgi?myfield=42&myfield=24

    Peter
    Peter Otten, Jan 26, 2007
    #2
    1. Advertising

  3. Christopher Mocock

    Paul Boddie Guest

    Christopher Mocock wrote:
    >
    > Bit of a python newbie so need a little help with a CGI script I'm
    > trying to write. I've got it working fine as long as the fields of the
    > form are filled in correctly, however I need to be able to accept blank
    > entries. Therefore I want to convert any empty entries to an empty string.


    [...]

    > form = cgi.FieldStorage()


    Try replacing this with...

    form = cgi.FieldStorage(keep_blank_values=1)

    Paul
    Paul Boddie, Jan 26, 2007
    #3
  4. Paul Boddie wrote:
    > Try replacing this with...
    >
    > form = cgi.FieldStorage(keep_blank_values=1)
    >
    > Paul
    >


    That worked a treat. Thanks very much to both who replied.

    Chris.
    Christopher Mocock, Jan 29, 2007
    #4
    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. ERobishaw
    Replies:
    1
    Views:
    800
    Anthony Jones
    Mar 22, 2008
  2. Matthew Margolis

    Ruby CGI and blank form fields

    Matthew Margolis, Jan 30, 2005, in forum: Ruby
    Replies:
    2
    Views:
    120
    Martin DeMello
    Jan 30, 2005
  3. Replies:
    7
    Views:
    152
    Michael Granger
    Feb 15, 2008
  4. Stefan Fischer
    Replies:
    2
    Views:
    246
    Stefan Fischer
    Feb 23, 2010
  5. Replies:
    23
    Views:
    479
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page