I'm tring to do my first web app using python 3.4 and Bottle.
I'm tring do create a dropdown list using data from one table colled "Documenti" (using sqlite) The table used has only two columns: ID_D (id) and Documents (name of document). My need is that when a value is choosed, and the botton "search" cliked, Bottle opens a new page, using the ID_D value from the first table and printing info from a second table (colled "Master") thet has also a column colled ID_D.
I'm tring to do this using these codes:
first page with dropdwon list (using first table)
@route('/doc')
def Document():
conn = sqlite3.connect('db.db')
c = conn.cursor()
result = c.execute("SELECT ID_D FROM documenti;").fetchall()
c.close()
output = template('doc', rows=result)
return output
Second page, opened using the ID_D from the previous page to print info from the second table
@route('/docx', method='GET')
def docx():
if request.GET.get('search','').strip():
id = request.GET.get('docx', '').strip()
conn = sqlite3.connect('db.db')
c = conn.cursor()
c.execute("SELECT * FROM master WHERE ID_D LIKE ?;" (id))
result = c.fetchall()
c.close()
output = template('doc3', rows=result)
return output
Here there is my html code used...
in the page with the dropdown list:
<form action="/docx" method="GET">
<select name="docx"><option selected="selected" value="">Seleziona</option>
%for row in rows:
<option value="{{row}}">{{row}}</option>
%end
</select>
<input type="submit" name="search" value="search">
In the second page (where i wont to see info from the second table using as filter ID_D:
<table border="1">
%for row in rows:
<tr>
%for col in row:
<td>{{col}}</td>
%end
</tr>
%end
</table>
My codes doesn't work...the second page is blank...
Thanks to all for your help! Mauro
EDIT: for everyone...this is my working code:
@route('/docx', method='GET')
def docx():
id = request.GET.get('docx', '').strip()
conn = sqlite3.connect('db.db')
c = conn.cursor()
result = c.execute("SELECT * FROM master WHERE ID_D LIKE (?)", (id,)).fetchall()
c.close()
output = template('doc3', rows=result)
return output
Thanks to all guys!
I believe that if request.GET.get('search','').strip():
is evaluating to False
and your view is thus returning None
, which is the default behavior in Python. This makes sense as a submit
button does not include itself in the query string. I would try this:
@route('/docx', method='GET')
def docx():
id = request.GET.get('docx', '').strip()
conn = sqlite3.connect('db.db')
c = conn.cursor()
c.execute("SELECT * FROM master WHERE ID_D LIKE ?;" (id))
result = c.fetchall()
c.close()
output = template('doc3', rows=result)
return output
If it still doesn't work, I would try right-clicking on the page (when opened in a browser) and selecting View Source
and you should see a blank page, if you see something like this:
<table border="1">
</table>
then the SQL query is wrong.