[英]How to create HTML table button for each row and input first column's value to Flask function for query
[英]Get HTML table row data to Flask function
我有一個 HTML 表,下面的模板中有幾列。 表格的最后一列有一個按鈕。 代碼如下。
索引.html
<table id="deployment_table" class="table table-striped">
<thead>
<tr>
{% for col in colnames %}
<th>{{ col }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for record in records %}
<tr>
{% for col in colnames %}
<td>{{ record[col] }}</td>
{% endfor %}
<td class="td-actions text-right">
<a href=ct_edit ><input type="submit" name="edit_btn" value="update"rel="tooltip" class="btn btn-info"></a>
</input>
</td>
</tr>
{% endfor %}
</tbody>
</table>
當用戶單擊特定行的按鈕時,我需要將所選行的第一列的值傳遞給 Flask function 並使用此數據加載另一個 HTML 頁面。
下面是 flask 后端 function。
@app.route('/ct_edit')
def ct_edit():
print('ct_edit')
# the value of the first column of the selected row => ID
# do something with the ID value and get a result
return render_template('ct_edit.html', result=result)
到目前為止,我無法完成這項工作。 請問對此我有什么建議嗎?
首先,請注意,當您的輸入標記關閉錨標記時,它應該是相反的,不確定它是否會影響本示例中的代碼,但最好保持它們的順序。
第二件事,Flask 有一個叫做url_for的東西,它可以幫助您利用 Jinja 模板,而不是硬編碼 html 的頁面名稱,您可以引用各個函數的名稱並傳遞特定的變量(如下所示 - 在您有您在 href = "{{ url_for(" function 的名稱") }}"中放置的參考或操作
在此示例中,由於您想要獲取特定記錄 - 為每條記錄指定一個特定值。 我放置了row["id"] ,它指的是表中該記錄的特定唯一值(我假設您有一些列對每一行都有唯一值 - 這應該放在 url_for 中)。
然后在Flask function ct_edit中負責顯示您通過單擊錨標記選擇的特定行,應該接收該唯一值作為參數(也將其放置在路由中,以便記錄可區分)。
<a href="{{ url_for("ct_edit", record_id = row["id"]) }}"><input type="submit" name="edit_btn" value="update"rel="tooltip" class="btn btn-info">
<i class="material-icons"></i></input></a>
@app.route('/ct_edit/<int:record_id>')
def ct_edit(record_id):
print('ct_edit')
# the value of the first column of the selected row => ID
# do something with the ID value and get a result
return render_template('ct_edit.html', result=result)
我希望我設法為你照亮了一點。
好好養:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.