隐藏页面特效

图书管理系统---基于ajax删除数据

1|0book_list.html代码


{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css' %}"> <link rel="stylesheet" href="{% static 'plugins/bootstrap-sweetalert-master/dist/sweetalert.css' %}"> </head> <body> <div class="container"> <h1>书籍展示</h1> <div class="row"> <div class="col-md-9 col-md-offset-2"> <a href="/add_book/" class="btn btn-primary">添加书籍</a> {# <a href="{% url 'add_book'%}" class="btn btn-primary">添加书籍</a>#} {# <a href="{% url 'add_book' 2020 12%}" class="btn btn-primary">添加书籍</a>#} <table style="margin-top: 10px;" class="table table-bordered table-striped table-hover"> <thead> <tr> <th>编号</th> <th>书籍名称</th> <th>价格</th> <th>出版日期</th> <th>出版社</th> <th>作者</th> <th>操作</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.title }}</td> <td>{{ book.price }}</td> <td>{{ book.pub_date|date:'Y-m-d' }}</td> <td>{{ book.pub.name }}</td> <td>{% for j in book.authors.values %} {{ j.name }} {% endfor %} </td> <td> <a href="{% url 'edit_book' book.id %}" class="btn btn-warning">编辑</a> <a href="{% url 'delete_book' book.pk %}" class="btn btn-danger">删除</a> <span style="display: none;">{{ book.pk }}</span> //隐藏了一个标签用于获取需要请求删除的id <a class="btn btn-success del">ajax删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> <script src="{% static 'js/jquery.js' %}"></script> <script src="{% static 'plugins/bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script> <script src="{% static 'plugins/bootstrap-sweetalert-master/dist/sweetalert.min.js' %}"></script> <script> $('.del').on('click',function(){ var delete_id = $(this).prev().text();//找这个点击标签的上一个标签 var ths = $(this); //用于在下面函数中使用这个点击标签 swal({ title: "are you sure?", text: "要谨慎!", type: "warning", showCancelButton: true, confirmButtonClass: "btn-danger", confirmButtonText: "确定删除", cancelButtonText: "容我三思", closeOnConfirm: false }, //点击确认删除时触发的函数 function(){ $.ajax({ url:'/ajax_delete_book/'+ delete_id +'/', //url:'/ajax_delete_book/'+ '100' +'/', //这个是用于演示错误响应error那个回调函数。 type:'get', success:function (res) { if (res==='ok'){ //关闭弹框 swal('删除成功!','你可以跑路了','success'); //局部刷新,删除对应记录 ths.parent().parent().remove(); }else{ console.log('出错啦'); }, error:function(res){ if (res.status===404){ swal('删除失败!',res.responseText,'error'); } } } }) } ) }) </script> </html>
  • urls.py文件
urlpatterns = [ url(r'^ajax_delete_book/(\d+)/', views.ajax_delete_book, name='ajax_delete_book'), ]
  • views.py文件
def ajax_delete_book(request,pk): try: models.Book.objects.get(pk=pk).delete() except: return HttpResponse('找不到对应的资源~~',status=404) return HttpResponse('ok')

__EOF__

本文作者404 Not Found
本文链接https://www.cnblogs.com/weiweivip666/p/13388711.html
关于博主:可能又在睡觉
版权声明:转载请注明出处
声援博主:如果看到我睡觉请喊我去学习
posted @   我在路上回头看  阅读(220)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示