本文介绍了如何在 Django 项目中,针对通过复选框选择的多个数据进行批量删除操作时,添加用户确认提示。通过 JavaScript 的 confirm() 方法,在用户点击删除按钮后弹出确认对话框,避免误操作导致的数据丢失,从而提升用户体验。
在 Django 项目中,实现批量删除功能时,为了防止用户误操作,通常需要在删除前添加一个确认提示。以下介绍如何利用 JavaScript 的 confirm() 函数实现这一功能。
实现步骤:
- 修改 HTML 模板:
在 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="https://www.php.cn/faq/{% url 'expens_delete' info.id %}"><i class="bi bi-x"></i></a> <a class="btn btn-warning btn-sm" href="https://www.php.cn/faq/{% url 'expens_edit' info.id %}"><i class="bi bi-pencil"></i></a> </td> </tr> {% endfor %} </tbody> </table> </form>
- 添加 JavaScript 代码:
在 HTML 模板的 <script> 标签中,添加以下 JavaScript 代码。该代码定义了一个名为 confirmDelete() 的函数,该函数会弹出一个确认对话框,询问用户是否确定删除。如果用户点击“确定”按钮,则返回 true,表单将被提交;否则,返回 false,表单不会被提交。</script>
<script> function confirmDelete() { return confirm("确定要删除选中的数据吗?"); } </script>
- 修改 views.py:
保持原有的逻辑不变。
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="https://www.php.cn/faq/{% url 'expens_delete' info.id %}"><i class="bi bi-x"></i></a> <a class="btn btn-warning btn-sm" href="https://www.php.cn/faq/{% 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 %}
注意事项:
- 确保在 HTML 模板中正确引入了 JavaScript 代码。
- 可以根据实际需求自定义确认对话框的内容。
- 如果需要更复杂的确认提示,例如自定义样式的模态框,可以使用 JavaScript 库(如 Bootstrap Modal)来实现。
- confirm() 函数是浏览器提供的内置函数,因此无需额外安装任何依赖。
总结:
通过以上步骤,可以在 Django 项目中轻松实现批量删除确认提示功能,有效地防止用户误操作,提升用户体验。这种方法简单易用,无需引入额外的依赖,适用于大多数 Django 项目。
本站资料仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END
暂无评论内容