我在index.html中有这个
{% for item in data %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
<td>{{item[4]}}</td>
<td>{{item[5]}}</td>
<td>{{item[6]}}</td>
<td>{{item[7]}}</td>
<td>{{item[8]}}</td>
<td>{{item[9]}}</td>
<td>{{item[10]}}</td>
<td>
<form action="{{ url_for('history') }}" method="POST">
<button type="submit"> History </button>
</form>
</td>
</tr>
{% endfor %}
这在app.py中:
@app.route('/history', methods=['GET', 'POST'])
def history():
return render_template('history.html')
因此,我的网页上有一个表,表中有一堆行,每一行都是一个标记为“历史记录”的按钮。既然现在每个按钮都执行相同的操作并指向history(),那么我该如何区分原始点击来自哪一行数据?
答案 0 :(得分:1)
您必须将项目ID添加到HTML表单中,但无法显示它。 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/hidden
类似以下内容
<form action="{{ url_for('history') }}" method="POST">
<input id="historicalId" name="historicalId" type="hidden" value="{{item.id}}">
<button type="submit"> History </button>
</form>
然后在flask中,您需要解析请求表单正文
答案 1 :(得分:1)
我正在写此答案,因为它可能会对某人有所帮助。 检查每个表格行是否都显示一本书,并在表格行末尾有一个编辑按钮。
{% for book in books %}
<tr>
<td>{{ book[1] }}</td>
<td>{{ book[2] }} </td>
<td>{{ book[3] }} </td>
<td>{{ book[4] }} </td>
<input id="book_id" name="book_id"
type="hidden" value="{{ book[0] }}">
<td><button type="submit" name="edit" value="{{ book[0] }}"
formmethod="post">Edit</button></td>
</tr>
每行都有一个隐藏的book_id,用于使用以下代码在后端检索图书:
if request.form.get("edit"):
print('Book ID: ', request.form.get('edit'))