i want to know what is the problem in this code

N

nani

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

Bruno Desthuilliers

nani a écrit :
i am getting the following error for below code
(snip)
C:\Program Files\Apache Group\Apache2\cgi-bin\hello.py in ()
7
8 val = cgi.FieldStorage()
9 name = val["name"].value (snip)

<type 'exceptions.KeyError'>: 'name'

Obviously there's no 'name' argument in the http request. Remember that
html forms dont return key/value pairs for empty (not selected /
whatever) fields.
 
G

Gerardo Herzig

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top