簡體   English   中英

如何為每一行創建 HTML 表格按鈕並將第一列的值輸入到 Flask 函數進行查詢

[英]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.

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