[英]How to create HTML table button for each row and input first column's value to Flask function for query
我有一个 HTML 表格,我希望在最后一列的每一行都有一个名为“更多详细信息”的按钮。
我希望能够在单击每一行的“更多详细信息”按钮时,该特定行的月份值(即表格的第一列,例如:'2021-10')将发送到一个仅查询特定月份信息的烧瓶函数,然后浏览器将跳转到另一个 HTML 文件 (monthly_sales_report_sub.html),该文件将新信息显示为包含一行的表格。
使 HTML 代码和 Flask 函数工作的最佳方法是什么? 我当前的代码将显示monthly_sales_report_sub.html 上的所有月份记录
提前致谢!
下面的 HTML 代码 (monthly_sales_report.html):
<table width=80%>
<!-- Show Report Table -->
<thead>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
<td class="td-actions text-right">
<a href=monthly_sales_report_sub><input type="submit" value="more details"><</input></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
我当前的烧瓶函数如下所示:
@api.route('/monthly_sales_report', methods=['GET'])
def monthly_sales_table():
return render_template("monthly_sales_report.html", headings=SQL_Operations.get_monthly_sales_report(connection)['headings'], data=SQL_Operations.get_monthly_sales_report(connection)['rows'])
@api.route('/monthly_sales_report_sub', methods=['GET'])
def monthly_sales_table_sub():
return render_template("monthly_sales_report_sub.html", headings=SQL_Operations.get_monthly_sales_report_sub(connection)['headings'], data=SQL_Operations.get_monthly_sales_report_sub(connection)['rows'])
SQL_Operations.py
def get_monthly_sales_report_sub(connection):
cursor = connection.cursor()
query = ("""SELECT month, sales_person, total_num_sold, total_sales FROM Sale;""")
cursor.execute(query)
res = []
for (month, sales_person, total_num_sold, total_sales) in cursor:
res.append([month, sales_person, total_num_sold, total_sales])
res_new = dict()
res_new['headings'] = ['month', 'sales_person', 'total_num_sold', 'total_sales']
res_new['rows'] = res
return res_new
在烧瓶中,您可以使用参数创建路由
@app.route('/detail/<month>',methods = ['GET'])
在您的表格中,您可以使用按钮或标签按钮:
<a href={{ url_for('detail',month = your_row_month_value) }} ...
您必须将 url_for 添加到 Flask 导入中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.