мои данные из базы данных, и я могу использовать «SELECT * FROM факторов ГДЕ Crash_Year = 2001/2002/2003» для отображения определенных c данных. но я не знаю, как добиться этой функции в моем html.
введите описание изображения здесь
Например, если я выберу 2002, будут отображаться год данных - 2002.
Я хочу использовать опциональный тег, и вот моя html кодировка:
{% block content %}
<article>
<form action="/show/" method="post">
<label for="year">Choose the rang of the Year:</label>
<select id="year" name="year">
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<input type="submit" value="Submit">
</form>
<table>
<tr>
<th>id</th>
<th>Crash Year</th>
<th>Region</th>
<th>Crash Severity</th>
<th>DrinkDriving</th>
<th>Driver Speed</th>
<th>Fatigued Driver</th>
<th>Defective Vehicle</th>
<th>Count Crashes</th>
<th>Count Fatality</th>
<th>Count Hospitalised</th>
<th>Count Medically Treated</th>
<th>Count Minor Injury</th>
<th>Count All Casualties</th>
</tr>
{% for row in factorsinfo %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.Crash_Year }}</td>
<td>{{ row.Crash_Police_Region }} </td>
<td>{{ row.Crash_Severity }}</td>
<td>{{ row.Involving_Drink_Driving }}</td>
<td>{{ row.Involving_Driver_Speed }}</td>
<td>{{ row.Involving_Fatigued_Driver }}</td>
<td>{{ row.Involving_Defective_Vehicle }}</td>
<td>{{ row.Count_Crashes }}</td>
<td>{{ row.Count_Fatality }}</td>
<td>{{ row.Count_Hospitalised }}</td>
<td>{{ row.Count_Medically_Treated }}</td>
<td>{{ row.Count_Minor_Injury }}</td>
<td>{{ row.Count_All_Casualties }}</td>
</tr>
{% endfor %}
</table>
</article>
{% endblock %}
, а вот мой app.py:
@app.route("/show", methods=['GET', 'POST'])
def show():
g.db = sqlite3.connect(DATABASE)
d = {}
d['id'] = ""
d['Crash_Year'] = ""
d['Crash_Police_Region'] = ""
d['Crash_Severity'] = ""
d['Involving_Drink_Driving'] = ""
d['Involving_Driver_Speed'] = ""
d['Involving_Fatigued_Driver'] = ""
d['Involving_Defective_Vehicle'] = ""
d['Count_Crashes'] = ""
d['Count_Fatality'] = ""
d['Count_Hospitalised'] = ""
d['Count_Medically_Treated'] = ""
d['Count_Minor_Injury'] = ""
d['Count_All_Casualties'] = ""
factorsinfo = []
cursor = None
try:
cursor = g.db.cursor()
rows = cursor.execute('SELECT * FROM factors ;')
for row in rows:
#print(row)
d = {} # necessary; weird ...
d['id'] = row[0]
d['Crash_Year'] = row[1]
d['Crash_Police_Region'] = row[2]
d['Crash_Severity'] = row[3]
d['Involving_Drink_Driving'] = row[4]
d['Involving_Driver_Speed'] = row[5]
d['Involving_Fatigued_Driver'] = row[6]
d['Involving_Defective_Vehicle'] = row[7]
d['Count_Crashes'] = row[8]
d['Count_Fatality'] = row[9]
d['Count_Hospitalised'] = row[10]
d['Count_Medically_Treated'] = row[11]
d['Count_Minor_Injury'] = row[12]
d['Count_All_Casualties'] = row[13]
factorsinfo.append(d)
except Exception as e:
return """<h1> Error occurred when accessing the database</h1>
<p>{}</p>""".format(e), 500
if cursor:
cursor.close()
g.db.close()
return render_template('show.html', factorsinfo=factorsinfo)