简体   繁体   English

我如何使用选项标签 select 数据库中的特定数据与 FLASK

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

my data from database, and I can use "SELECT * FROM factors WHERE Crash_Year = 2001/2002/2003" to display specific data.我的数据来自数据库,我可以使用“SELECT * FROM factors WHERE Crash_Year = 2001/2002/2003”来显示特定数据。 but I don't know how to achieve this function in my html.但我不知道如何在我的 html 中实现这个 function。

enter image description here在此处输入图像描述

For example, if I select 2002, there are display the year of data are 2002.例如,如果我 select 2002,则显示数据的年份为 2002。

I want to use option tag and here are my html coding:我想使用选项标签,这是我的 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 %}

and here is my app.py:这是我的 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)

Perhaps the problem statement could be:也许问题陈述可能是:

If route is called from a POST request, and user has selected a "year", only send that year's data to the html如果从 POST 请求调用路由,并且用户选择了“年份”,则仅将该年份的数据发送到 html

One way to solve that would be to only send the selected year's data back to the html here解决此问题的一种方法是仅将选定年份的数据发送回此处的 html
return render_template('show.html', factorsinfo=factorsinfo)

Before the return, you will need a routine to collect and send the data from factorsinfo that contains the selected year.在返回之前,您将需要一个例程来从包含所选年份的factorsinfo中收集和发送数据。 You will need to use flask's request.method and request.form .您将需要使用烧瓶的request.methodrequest.form In pseudo code:在伪代码中:

  • Is it a post request?是post请求吗? (ie request.method == 'POST') (即request.method == 'POST')
  • Is a year selected?选择年份了吗? (ie request.form['year'] is not None ) (即request.form['year'] is not None
  • Find the data in factorsinfo for that yearfactorsinfo中查找当年的数据
  • Return that set to the render_template将该集合返回到render_template

It could be done in the html, but the aforementioned return would have to also send the year that was selected, and an if clause would need to added after this {% for row in factorsinfo %} clause in the html.可以在 html 中完成,但上述return还必须发送选择的年份,并且需要在 html 中的{% for row in factorsinfo %}子句之后添加if子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么能得到 <select>用户使用Flask选择的标签选项? - How can I get the <select> tag option chosen by the user using Flask? 如何从我使用 select 选项的数据库中获取数据,以便它可以显示在输入框中 - How to get a data from database where i using select option so it can be shown in inputbox 通过选择选项标签从数据库中获取数据,以便在选择“全部”数据库时应选择“ *” - Fetching data from database through select option tag such that when select 'all' database should select ' * ' 如何在select-option标记中显示mysql数据库中的值 - How to display a value from mysql database in select-option tag 如何从数据库中获取项目的ID<select><option>标签 - How to get ID of item from database in <select><option> Tag 如何使用选择菜单在HTML表格中显示SQL数据库中的特定结果 - How do i display specific results from SQL database in a HTML TABLE using select menus 使用多页选择选项标签从mysql排序数据 - Sort data from mysql using select option tag for multiple page 如何更改选项标签中特定文本的颜色 - How do I change color of specific text in option tag 在 HTML 表单中,如何通过 optgroup 的 PHP 访问 select 标记中的选项? - In a HTML form how can I do to access via PHP a option from select tag by optgroup? 如何在选择标签中添加虚线作为选项? - How do I add a dotted line as an option in a select tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM