html - 如何使用 Flask 从 HTML 表中删除数据
问题描述
我有一个从 Flask 应用程序填充的 HTML 表。填充表格时,我在每一行中插入一个删除按钮。该按钮有一个返回 Flask 应用程序的链接,并包含记录的唯一 ID 号。加载表格后,我可以检查浏览器中的按钮。网址是我所期望的。 <a href="/processDeleteUser/10" class="btn btn-danger" role="button">Delete</a>
但是,当我单击该按钮时,它会将不同的 ID 传递给 Flask 应用程序<a href="/processDeleteUser/7"
并删除表的第一行而不是预期的行。请在下面找到我的代码:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
{% for u in users %}
{% set uid = u[0] %}
{% set first = u[2] %}
{% set last = u[3] %}
{% set email = u[4] %}
{% set password = u[5] %}
<tr>
<td>{{uid}}</td>
<td>{{last}}, {{first}}</td>
<td>{{email}}</td>
<td>{{password}}</td>
<td>
<!-- Button trigger modal -->
<button type="button" class="btn btn-light" data-bs-toggle="modal" data-bs-target="#confirmModal">
<i style="color:red" class="fa fa-times fa-1x"></i>
</button>
<!-- Modal -->
<div class="modal fade" id="confirmModal" tabindex="-1" aria-labelledby="confirmModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirmModalLabel">Confirm Delete</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete this user?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<a href="/processDeleteUser/{{uid}}" class="btn btn-danger" role="button">Delete</a>
</div>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
解决方案
我认为错误在循环内部,您在其中多次声明具有相同 id 的模式。onclick
我建议您通过按钮事件调用的 javascript 函数来管理取消
<tbody>
{% for u in users %}
{% set uid = u[0] %}
{% set first = u[2] %}
{% set last = u[3] %}
{% set email = u[4] %}
{% set password = u[5] %}
<tr>
<td>{{uid}}</td>
<td>{{last}}, {{first}}</td>
<td>{{email}}</td>
<td>{{password}}</td>
<td>
<!-- Button trigger modal -->
<button type="button" class="btn btn-light" onclick="myFunction({{uid}})">
<i style="color:red" class="fa fa-times fa-1x"></i>
</button>
</td>
</tr>
{% endfor %}
</tbody>
<script>
function myFunction(id) {
// handle the cancellation
}
</script>
推荐阅读
- python - 如何使用 xlrd 将 excel 文件中的所有行上传到 django 模型中
- google-sheets-formula - 谷歌表格:雅虎财经的 IMPORTXML 对某些股票失败
- vba - VBA循环检查日期时间是否在另一个工作表上的2个日期时间单元格之间
- c# - 使用 C# 重定向从批处理文件调用的可执行文件的输入和输出
- python - 使用 wavfile.read() 时音频文件的输出不是从零开始的
- airflow - AirFlow 没有运行计划
- javascript - 如何复制Dropdownlist的值,减去一个值(默认为0)并计算总和,只需点击Dropdownlist?
- powershell - 使用 powershell 远程安装 windows 更新
- machine-learning - 一旦学习了 T(s,a,s') 和 R(s,a,s'),是否可以仅根据学习模型中的信息使用值迭代来找到最优策略?
- php - 在 Ajax 代码和 PHP 文件之间传递变量的新正确方法是什么?