繁体   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