[Tutor] Re: How can I code to get more than One value from checkbox?

Ms Soo Chong s4046441 at student.uq.edu.au
Fri Oct 1 17:13:56 CEST 2004


----- Original Message -----
From: Steve Holden <steve at holdenweb.com>
Date: Friday, October 1, 2004 10:20 pm
Subject: Re: How can I code to get more than One value from checkbox?

> > The following is my script:

> ###############################################################################> #!/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>
> > 
#######
> OK, it's not really obvious what this form is supposed to do for 
> you. I 
> also don't understand whether it makes sense for "all" to be 
> checked at 
> the same time as individual selections. But we'll leave that for 
> now ...
######
> 
> >              <p>Search by shot number:<br>
> >              <input type=text name="qtext" value="" />
> 
######
> It might make more sense to call this input "shotno" rather than 
> "qtext", but that won;t affect how the code functions.
######
> 
> >              <input type="submit" value="SEARCH" /> 
> >              <input type="reset" value="RESET" /><br />
> >              </form>"""
> > 
######
> Aah, so we're looking up photographs in a database?
######
> 
> > print "Content-Type: text/html\n\n"
> > print "<head><title>Searching by using Shot
> > Number</title></head><body>"
> > 
> > if __name__ == "__main__":
> > 
######    
> It's a bit too late for this if you've already written the headers 
> out, 
> I should say - it would make more sense to move those prints above 
> down 
> here so they don't get run if the script is imported.
######
> 
> >     data = cgi.FieldStorage()
> > 
> Great!
> 
> >     if data:
> >         qtype = data.getfirst('qtype')
> 
######
> Since the "for" statement below assigns to qtype, the statement 
> above is 
> completely redundant.
######
> 
> >         qtext = data.getfirst('qtext','')
> > 
> >         for qtype in data.getlist('qtype'):
> 
#####
> So if you check "project" and "blame", you should iterate round 
> this 
> loop twice, with qtype being set to "project" the first time and 
> "blame" 
> the second.
#####
> 
> >             print "You typed in Shot Number:", qtext
> > 
> >             #Will take care of the security problem later...
> >             username = os.environ.get('USER')
> >             if username == None:
> >                 username = 'apache'
> > 
> >             #Now, we can get to the database...
> >             db = pg.connect("moncdata", user=username, passwd=None)
> > 
#####
> It doesn't make sense to connect to the database each time round 
> the 
> loop (and I'm still not even convinced this loop is what you want...)
#####
> 
> >             #This is the part that I'm not sure of...please help...
> >             query = "select %(qtype)s from shot_descriptions where
> > shot_number=%(qtext)s", qtype, qtext
> >             qresult = db.query(query)
> > 
#####
> I take it pg is some kind of PostGres interface? And AAH!!, now I 
> see 
> that what the checkboxes are supposed to do is select fields for 
> the 
> output. You appear to be missing a "percent" sign up there - it 
> should 
> either be
> 
>     query = "SELECT %s ...=%s" % (qtype, qtext)
> 
> or
> 
>     query = "SELECT %(qtype)s ... =%(qtext)s" % data
> 
> to put the query type and the text into the string, depending on 
> which 
> substitution method you want to use. You should really be testing 
> a lot 
> of this stuff in an interactive copy of the interpreter (I hope 
> you do 
> have Python available other than just on your web  server).
> 
> Unfortunately the assignment to query isn;t a syntax error in 
> Python 
> (you are just assigning a three-element tuple to query) but poor 
> old 
> PostgreSQL isn;t going to know what to make of the result.
> 
> Do you want to see a separate chunk of output for each field, or 
> do you 
> want to see the fields as columns of a table? The latter seems to 
> make 
> more sense to me, but it would have been REALLY nice if this 
> conversation has started out like "I have a bunch of shot 
> descriptions 
> is a database, and I want to be able to see just selected fields 
> about a 
> shot".
> 
> For the structure you've got now it looks like you plan to loop 
> around 
> the column names, doing a query for each one, and printing out a 
> table 
> for each field. And we don't have any way to handle "all", by the 
> way 
> ... unless that's just another database column.
######
> 
> >             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()
> > 
> >         #Somehow, this part didn't work too, when I didn't input a
> > value
> >         #it doesn't show this msg. Please help me on this part too.
> > Thks.
> 
#####
> The reason for that is that even when you don't enter anything 
> into the 
> form the submit button still gets put in there as a data item, so 
> the 
> form will never be truly empty.
#####
> 
> >         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>"
> >     
> > 
> ###############################################################################> 
> > Thank you in advance for any help.
> > 
> > Shufen
> 
> OK, before we go any further I'd like you to tell me exactly what 
> you;d 
> like this page to do. You probably aren't that far froma solution 
> now, 
> but it would be nice to answer the right question.
> 
> I'm suspecting that what you really want is something like
> 
>     query = """SELECT %s FROM shot-descriptions WHERE
>                   shot_number=%s""" % (
>                	data.getvalue("qtype"), data.getfirst("qtext"))
> 
> Since I have to go off and earn pennies now, someone else may be 
> able to 
> help you in the interim. I'm likely to be off-net for a bit. Stick 
> with 
> it, you're almost there!
> 
> regards
>  Steve

 
A big big thank you to Steve. 

Thanks Steve, you have been a great help... rite! you have pointed out some of the stuffs that I myself have problems detecting them, thanks. I really hope that "I'm almost there!" since I have to complete this within this two weeks.

I think you are right, sorry that I didn't explain my situation well enough. I will do that now.
I have a bunch of stuffs in a database table named "shot_descriptions". And this is what I wanna my page to do:

The columns of this table consists of project name, date, shot number, notes and etc. And these fields are the stuffs that I wanna to present them in checkboxes form so that the user can select the number of information they wanna to see in the results (output). And a textbox for user to key in a specific shot number in order to perform a search. So, if there is no input in the shot no textbox, an error msg will show (this is one of the part that is not working now, please help). I wanna to display the output in a table form and I'm currently using dictionary to help me with this task. But I'm wondering is there a better way to show it? I mean other than dictionary, how can I code it so that the results will display nicely in a html type of table with columns and rows instead of key: blah blah and value: blah blah. If someone can help me on this part, it will be very nice, cos I'm concern about the presentation of the output too.

I will try out the suggestion given by steve and send out the revised copy of my code but at the mean time, it would be great if someone understand what am I trying to do here and send me more suggestions on the correct way of dealing with this type of situation. Thank you in advance. I'm really keen to learn more stuffs about python, esp ways to improve the code to make it more efficient and clean. I'm also anxious in seeing the outcomes of this web page so please help me up.

Any help is very very very much appreciated.


p/s: To Steve, Lee Harr and Danny Yoo, Thanks a million. I really appreciated the trouble you guys gone thru to read and analyse my code since I didn't seems to provide you with enough info (sorry for that). 

Cheers,
Shufen









More information about the Tutor mailing list