[Tutor] Sorry, Please advice me on this code
Ms Soo Chong
s4046441 at student.uq.edu.au
Thu Sep 30 05:42:59 CEST 2004
Thanks to Lee Harr and Danny Yoo for the help. It is
very much appreciated.
Sorry, I know what I wanna to do but don't really
know how to implement it in the code, so I need help
again, sorry for that.
As attached, shot_no1 & shot_no2, I changed :
####
qtype = data['qtype'].value
try:
qtext = data['qtext'].value
except KeyError:
qtext = ''
###
to
###
qtype = data.getfirst('qtype')
qtext = data.getfirst('qtext', '')
###
but I still cant' quite get it rite.
In shot_no1, I have to precode every single option (
checkedbox) the user ticked, which then my script
will be very very long.(and I don't want it to be
the case)
In shot_no2, I try to code it in the way that the
script get the value from the form and process it
which have some errors in it (I know that the way
that I do is wrong). Thus, can someone please help
me to scan thru both of them and advice me on how
should I code it so that I can keep my script clean
and more readable.
Sorry for all the trouble. I'm really not good in
programming and still very new to it.
Thanks for any help.
Cheers,
Shufen
-------------- next part --------------
#!/usr/bin/env python
#Created on: 30/09/04
#Help from Lee Harr and Danny Yoo - Python Tutor
import sys, os
import cgi
import cgitb; cgitb.enable()
import pg
def form():
print """<form method="post" action="">
<p>
<input type=checkbox name="qtype" value="all" checked />
All<br />
<input type=checkbox name="qtype" value="project" />
Project<br />
<input type=checkbox name="qtype" value="date_string" />
Date<br />
<input type=checkbox name="qtype" value="blame" />
Blame<br />
<input type=checkbox name="qtype" value="notes" />
Notes<br /></p>
<p>Search by shot number:<br>
<input type=text name="qtext" value="" />
<input type="submit" value="SEARCH"><br>
</form>"""
print "Content-Type: text/html\n\n"
print "<head><title>Searching by using Shot Number</title></head><body>"
if __name__ == "__main__":
data = cgi.FieldStorage()
if data:
qtype = data.getfirst('qtype')
qtext = data.getfirst('qtext','')
if qtype == "all":
print "You typed in Shot Number:", qtext
#Now, we can get to the database...
username = os.environ.get('USER')
if username == None:
username = 'apache'
db = pg.connect("moncdata", user=username, passwd=None)
query = "select * from shot_descriptions where shot_number=%(qtext)s" % {'qtext': qtext}
qresult = db.query(query)
listOfResults = qresult.dictresult()
print """<p>Example of pulling the list of dictionary results apart.</p>"""
for record in listOfResults:
print "<p><table>"
for k in record.keys():
print '<tr>'
print '<td>key:</td> <td>', k, '</td>'
print '<td>value:</td><td>', record[k], '</td>'
print '</tr>'
print '</table></p>'
db.close()
elif qtype == "project":
print "You typed in Shot Number:", qtext
#Now, we can get to the database...
username = os.environ.get('USER')
if username == None:
username = 'apache'
db = pg.connect("moncdata", user=username, passwd=None)
query = "select project from shot_descriptions where shot_number=%(qtext)s" % {'qtext': qtext}
qresult = db.query(query)
listOfResults = qresult.dictresult()
print """<p>Example of pulling the list of dictionary results apart.</p>"""
for record in listOfResults:
print "<p><table>"
for k in record.keys():
print '<tr>'
print '<td>key:</td> <td>', k, '</td>'
print '<td>value:</td><td>', record[k], '</td>'
print '</tr>'
print '</table></p>'
db.close()
elif qtype == "date_string":
print "You typed in Shot Number:", qtext
#Now, we can get to the database...
username = os.environ.get('USER')
if username == None:
username = 'apache'
db = pg.connect("moncdata", user=username, passwd=None)
query = "select date_string from shot_descriptions where shot_number=%(qtext)s" % {'qtext': qtext}
qresult = db.query(query)
listOfResults = qresult.dictresult()
print """<p>Example of pulling the list of dictionary results apart.</p>"""
for record in listOfResults:
print "<p><table>"
for k in record.keys():
print '<tr>'
print '<td>key:</td> <td>', k, '</td>'
print '<td>value:</td><td>', record[k], '</td>'
print '</tr>'
print '</table></p>'
db.close()
elif not qtext:
print "You have no enter a shot number!"
print "Please type a in shot number in order to perform a search!"
else:
form()
print "</body></html>"
-------------- next part --------------
#!/usr/bin/env python
#Created on: 30/09/04
#Help from Lee Harr and Danny Yoo - Python Tutor
import sys, os
import cgi
import cgitb; cgitb.enable()
import pg
def form():
print """<form method="post" action="">
<p>
<input type=checkbox name="qtype" value="all" checked />
All<br />
<input type=checkbox name="qtype" value="project" />
Project<br />
<input type=checkbox name="qtype" value="date_string" />
Date<br />
<input type=checkbox name="qtype" value="blame" />
Blame<br />
<input type=checkbox name="qtype" value="notes" />
Notes<br /></p>
<p>Search by shot number:<br>
<input type=text name="qtext" value="" />
<input type="submit" value="SEARCH" />
<input type="reset" value="RESET" /><br />
</form>"""
print "Content-Type: text/html\n\n"
print "<head><title>Searching by using Shot Number</title></head><body>"
if __name__ == "__main__":
data = cgi.FieldStorage()
if data:
qtype = data.getfirst('qtype')
qtext = data.getfirst('qtext','')
for qtype in data.getlist('qtype'):
print "You typed in Shot Number:", qtext
#Now, we can get to the database...
username = os.environ.get('USER')
if username == None:
username = 'apache'
db = pg.connect("moncdata", user=username, passwd=None)
query = "select %(qtype)s from shot_descriptions where shot_number=%(qtext)s", qtype, qtext
qresult = db.query(query)
listOfResults = qresult.dictresult()
print """<p>Example of pulling the list of dictionary results apart.</p>"""
for record in listOfResults:
print "<p><table>"
for k in record.keys():
print '<tr>'
print '<td>key:</td> <td>', k, '</td>'
print '<td>value:</td><td>', record[k], '</td>'
print '</tr>'
print '</table></p>'
db.close()
else:
print "You have no enter a shot number!"
print "Please type a in shot number in order to perform a search!"
else:
form()
print "</body></html>"
More information about the Tutor
mailing list