[Tutor] CGI problem
David Holland
davholla2002 at yahoo.co.uk
Mon Jan 10 23:31:21 CET 2005
I am trying to write a CGI program here is the code of
the HTML
<FORM ACTION="cgi-bin/party2.py" METHOD="POST">
<P><SELECT NAME="language">
<OPTION SELECTED>ALL
<OPTION>Bromley
<OPTION>Lewisham</OPTION>
Here is the python
#!/usr/bin/env python
boroughdict = {
'BROMLEY': 0.5,
'LEWISHAM':0.1
}
class dummy: #mocked up input obj
def __init__(self, str):
self.value = str
import cgi, string
form = cgi.FieldStorage()
def showboroughinfo(form):
try:
choice = form[inputkey].value
except:
choice = 'BROMLEY'
print "not getting from form"
choice = string.upper(choice)
info = boroughdict[choice]
#this is not being called
newinfo = calcinfo(info)
print "<H3></H3><P><PRE>"
print cgi.escape (newinfo)
print "</PRE></P><BR>"
#print '<HR>'
def calcinfo(info):
#this is calcuate stuff
info = str(info)
return info
print "Content-type: text/html\n"
print "<TITLE>Languages</TITLE>"
print "<H1>This will happen</H1><HR>"
showboroughinfo(form)
#print "Hi"
print '<HR>'
For some reason it is not get the info in the try
block.
If I do not have the try block I get this message :-
Traceback (most recent call last):
File
"/home/david/Documents/pyprogramming/cgi-bin/party2.py",
line 45, in ?
showboroughinfo(form)
File
"/home/david/Documents/pyprogramming/cgi-bin/party2.py",
line 19, in showboroughinfo
choice = form[inputkey].value
File "/usr/lib/python2.3/cgi.py", line 552, in
__getitem__
raise KeyError, key
KeyError: 'borough'
Why is the input key not working ?
Thanks in advance.
___________________________________________________________
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
More information about the Tutor
mailing list