簡體   English   中英

我如何使用選項標簽 select 數據庫中的特定數據與 FLASK

[英]how do I using option tag to select the specific data from database with FLASK

我的數據來自數據庫,我可以使用“SELECT * FROM factors WHERE Crash_Year = 2001/2002/2003”來顯示特定數據。 但我不知道如何在我的 html 中實現這個 function。

在此處輸入圖像描述

例如,如果我 select 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)

也許問題陳述可能是:

如果從 POST 請求調用路由,並且用戶選擇了“年份”,則僅將該年份的數據發送到 html

解決此問題的一種方法是僅將選定年份的數據發送回此處的 html
return render_template('show.html', factorsinfo=factorsinfo)

在返回之前,您將需要一個例程來從包含所選年份的factorsinfo中收集和發送數據。 您將需要使用燒瓶的request.methodrequest.form 在偽代碼中:

  • 是post請求嗎? (即request.method == 'POST')
  • 選擇年份了嗎? (即request.form['year'] is not None
  • factorsinfo中查找當年的數據
  • 將該集合返回到render_template

可以在 html 中完成,但上述return還必須發送選擇的年份,並且需要在 html 中的{% for row in factorsinfo %}子句之后添加if子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM