I have a flask web app that contains a dropdown list that will allow the user to select which shoe they want and I would like to query the database using the dropdown list and display the shoe information and image but I can't quite work out the flow/logic of how I should go about it. I've only been using flask and SQLite for a few weeks so I would appreciate any advice! Dropdown list in /choosebrand and the corresponding image from the DB should be displayed in the next page, /analyse.
EDIT: I forgot to mention that the image is stored as a BLOB in the DB but I also have the images in the static folder. If there is even an easier way where I can just use the dropdown list to search for the filename in that folder instead of querying the database, I would much appreciate it :)
main.py:
#CHOOSE BRAND:
@app.route('/choosebrand')
def choosebrand():
return render_template('choosebrand.html')
# ANALYSE SHOE (also displays feature matching results):
@app.route('/analyse', methods=['GET', 'POST'])
def analyse():
#displays dropdown info inputted by user
brandName = request.form['brandName']
model = request.form['model']
colourway = request.form['colourway']
return render_template('analyseshoe.html', brandName=brandName, model=model, colourway=colourway)
choosebrand.html dropdown list:
<form class="shoelist" method="POST" action="{{ url_for('analyse') }}">
<div>
<label>Brand</label>
<select id = "brandName" name="brandName">
<option value = "">--SELECT--</option>
<option value = "Yeezy">Yeezy</option>
getBrandName()
</select>
</div>
<br>
<div>
<label>Model</label>
<select id = "model" name="model">
<option value = "">--SELECT--</option>
<option value = "Boost 350">Boost 350</option>
<option value = "Boost 500">Boost 500</option>
</select>
</div>
<br>
<div>
<label>Colourway</label>
<select id = "colourway" name="colourway">
<option value = "">--SELECT--</option>
<option value = "Cloud White">Cloud White</option>
<option value = "Zebra">Zebra</option>
<option value = "Beluga V2">Beluga V2</option>
<option value = "Black">Black</option>
<option value = "Citrin">Citrin</option>
<option value = "Triple White">Triple White</option>
<option value = "Yecheil">Yecheil</option>
<option value = "Yeshaya">Yeshaya</option>
<option value = "Bone White">Bone White</option>
<option value = "Salt">Salt</option> <option value = "Soft Vision">Soft Vision</option>
<option value = "Stone">Stone</option>
<option value = "Utility Black">Utility Black</option>
</select>
</div>
<br>
<div>
<input type="submit" class="submit" value="Analyse Shoe">
</div>
</form>
analyseshoe.html:
<div>
<h1 class="heading">Analyse Shoe</h1>
<p class="selection">Authenticity of your {{brandName}} {{model}} {{colourway}}:</p>
</div>
Shoe.Db:
try:
sqliteConnection = sqlite3.connect('Shoe.db')
sqlite_create_table_query = ''' CREATE TABLE ShoeDetails (
shoeId INTEGER PRIMARY KEY AUTOINCREMENT,
brandName TEXT NOT NULL,
model TEXT NOT NULL,
colourway TEXT NOT NULL,
shoeImage BLOB NOT NULL); '''
getBrandName()in HTML ? You can't run Python code in HTML. You can only use{% %}to use some code in HTML to generate HTML which is send to browser. Browser will send dropdown value toanalyse()and it should useSQLtoSELECTvalue from database and send it in new template.analyse()after gettingrequest.form[...]and beforerender_template()you have to useSELECT * FROM ShoeDetails WHERE ...to get data from database. First: you have to know how to write SQL queries. Second: you have to use this query withsqliteConnection.execute(query, (arg1, arg2, arg2,...))