i want to know what is the problem in this code

Gerardo Herzig gherzig at fmed.uba.ar
Mon Nov 26 07:47:23 EST 2007


nani wrote:

>i am getting the following error for below code
>
><type 'exceptions.KeyError'>	Python 2.5.1: C:\Python25\python.exe
>Mon Nov 26 10:13:17 2007
>
>A problem occurred in a Python script. Here is the sequence of
>function calls leading up to the error, in the order they occurred.
> C:\Program Files\Apache Group\Apache2\cgi-bin\hello.py in ()
>    7
>    8 val = cgi.FieldStorage()
>    9 name = val["name"].value
>   10 time_from = val["time_from"].value
>   11 time_to = val["time_to"].value
>name undefined, val = FieldStorage(None, None, []), ].value = []
> C:\Python25\lib\cgi.py in __getitem__(self=FieldStorage(None, None,
>[]), key='name')
>  565             if item.name == key: found.append(item)
>  566         if not found:
>  567             raise KeyError, key
>  568         if len(found) == 1:
>  569             return found[0]
>builtin KeyError = <type 'exceptions.KeyError'>, key = 'name'
>
><type 'exceptions.KeyError'>: 'name'
>
>
>
>#!C:/Python25/python.exe
>import cgi
>import cgitb; cgitb.enable()
>
>print "Content-Type: text/html"
>print
>
>val = cgi.FieldStorage()
>name = val["name"].value
>time_from = val["time_from"].value
>time_to = val["time_to"].value
>html = """
><html>
>
>    <body>
>        <h1> Hello %s  from %s to %s </h1>
>    </body>
>
></html>
>"""
>print html%(name, time_from, time_to)
>
>
>
>
>
>
>html page is.......................................
>
>
><html>
>
>
><body>
>
>    <form action="C:/Program Files/Apache Group/Apache2/cgi-bin/
>hello.py">
>        <table>
>            <tr>
>                <td> Enter your name: </td>
>                <td> <input type="text" name="name" value="" /> </td>
>            </tr>
>             <tr>
>                <td> Time from: </td>
>                <td> <input type="text" name="time_from" value="" /> </
>td>
>            </tr>
>              <tr>
>                <td> Time to: </td>
>                <td> <input type="text" name="time_to" value="" /> </
>td>
>            </tr>
>            <tr >
>                <td colspan="2" align="center">  <input type="submit"
>value="Click Here" /> </td>
>            </tr>
>        </table>
>
>    </form>
>
></body>
>
></html>
>
>  
>
Looks like the text field "name" is empty when "submitin" the form, and 
cgi.FieldStorage() -no args- does not build the key/pair for empty 
textfields. You can use the keep_blank_values=1 arg in FieldStorage() if 
you want force the key/pair to be generated anyway.

Cheers.
Gerardo



More information about the Python-list mailing list