将按钮按下与表格行烧瓶/Python关联
我在index.html中有以下内容:将按钮按下与表格行烧瓶/Python关联,python,html,python-3.x,flask,button,Python,Html,Python 3.x,Flask,Button,我在index.html中有以下内容: {% for item in data %} <tr> <td>{{item[0]}}</td> <td>{{item[1]}}</td> <td>{{item[2]}}</td> <td>{{item[3]}}<
{% 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 %}
因此,我的网页有一个表,表中有一组行,每一行都有一个标记为“History”的按钮。由于现在每个按钮都执行相同的操作并指向history(),我如何区分原始单击来自哪一行数据?您必须将项目ID添加到HTML表单中,但无法显示它 类似于下面的内容
<form action="{{ url_for('history') }}" method="POST">
<input id="historicalId" name="historicalId" type="hidden" value="{{item.id}}">
<button type="submit"> History </button>
</form>
历史
然后在flask中,您需要解析出请求表单body我正在写这个答案,因为它可能会有帮助。 检查每个表格行是否显示一本书,以及表格行末尾是否有编辑按钮
{% 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>
为什么不在表单中创建select字段呢?对于表,您可以多次重复几乎相同的代码。表单是用来向服务器传递数据的,表只是用来以表格的顺序表示某些东西。。。
{% 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>
if request.form.get("edit"):
print('Book ID: ', request.form.get('edit'))