
本文介绍了如何在 Django 项目中,针对通过复选框选择的多个数据进行批量删除操作时,添加用户确认提示。通过 JavaScript 的 confirm() 方法,在用户点击删除按钮后弹出确认对话框,避免误操作导致的数据丢失,从而提升用户体验。
在 Django 项目中,实现批量删除功能时,为了防止用户误操作,通常需要在删除前添加一个确认提示。以下介绍如何利用 JavaScript 的 confirm() 函数实现这一功能。
实现步骤:
在 HTML 模板中,找到批量删除按钮,并添加一个 onclick 事件处理程序。该事件处理程序会调用 confirm() 函数,根据用户的选择决定是否提交表单。
<form method="post" id="deleteForm">
{% csrf_token %}
<table class="table table-success table-striped" id="table">
<thead>
<th>#</th>
<th>Assignments</th>
<th>Amount</th>
<th>Date</th>
<th>
<button class="btn btn-danger btn-sm" name="delete_all" onclick="return confirmDelete()">Delete selected</button>
</th>
</thead>
<tbody>
{% for info in data %}
<tr>
<td><input type="checkbox" name="x[]" value="{{info.id}}"> {{forloop.counter}}</td>
<td>{{info.assignment}}</td>
<td>{{info.amount}}</td>
<td>{{info.add_date}}</td>
<td>
<a class="btn btn-danger btn-sm" href="{% url 'expens_delete' info.id %}"><i class="bi bi-x"></i></a>
<a class="btn btn-warning btn-sm" href="{% url 'expens_edit' info.id %}"><i class="bi bi-pencil"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>在 HTML 模板的 <script> 标签中,添加以下 JavaScript 代码。该代码定义了一个名为 confirmDelete() 的函数,该函数会弹出一个确认对话框,询问用户是否确定删除。如果用户点击“确定”按钮,则返回 true,表单将被提交;否则,返回 false,表单不会被提交。
<script>
function confirmDelete() {
return confirm("确定要删除选中的数据吗?");
}
</script>保持原有的逻辑不变。
def expens(request):
data = ''
number = ''
if 'delete_all' in request.POST:
choosen = request.POST.getlist('x[]')
if choosen:
for selected in choosen:
picked = Expenses.objects.filter(id=selected)
picked.delete()
messages.info(
request, "Expens data has been deleted successfully.", extra_tags='success')
else:
messages.info(request, "Please select to delete.",
extra_tags='error')
if 'save' in request.POST:
pass # Handle save logic if needed
return render(request, 'expens.html', {'data': data, 'number': number})完整示例代码:
{% extends 'base.html' %}
{% block content %}
<form method="post" id="deleteForm">
{% csrf_token %}
<table class="table table-success table-striped" id="table">
<thead>
<th>#</th>
<th>Assignments</th>
<th>Amount</th>
<th>Date</th>
<th>
<button class="btn btn-danger btn-sm" name="delete_all" onclick="return confirmDelete()">Delete
selected</button>
</th>
</thead>
<tbody>
{% for info in data %}
<tr>
<td><input type="checkbox" name="x[]" value="{{info.id}}"> {{forloop.counter}}</td>
<td>{{info.assignment}}</td>
<td>{{info.amount}}</td>
<td>{{info.add_date}}</td>
<td>
<a class="btn btn-danger btn-sm" href="{% url 'expens_delete' info.id %}"><i
class="bi bi-x"></i></a>
<a class="btn btn-warning btn-sm" href="{% url 'expens_edit' info.id %}"><i
class="bi bi-pencil"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<script>
function confirmDelete() {
return confirm("确定要删除选中的数据吗?");
}
</script>
{% endblock %}注意事项:
总结:
通过以上步骤,可以在 Django 项目中轻松实现批量删除确认提示功能,有效地防止用户误操作,提升用户体验。这种方法简单易用,无需引入额外的依赖,适用于大多数 Django 项目。
以上就是Django 中实现批量删除确认提示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号