django模糊搜索和分页和批量删除

视图

#搜索
       search_keywords = request.GET.get('keywords','')
       start = request.GET.get('start','')
       end = request.GET.get('end','')
       if start and end :
           all_Equip = all_Equip.filter(Q(add_time__range=(start,end))) # add_time字段
       if search_keywords:
           # 在name字段进行操作,做like语句的操作。i代表不区分大小写
           # or操作使用Q
           all_Equip = all_Equip.filter(
               Q(name=search_keywords) | Q(mac=search_keywords) | Q(version=search_keywords) | Q(
                   sn_num=search_keywords) | Q(con_state=search_keywords) | Q(ex_state=search_keywords))
       try:
           page = request.GET.get('page',1)
       except PageNotAnInteger:
           page = 1
       p = Paginator(all_Equip,2,request=request)
       equips = p.page(page)
       return render(request,'./equipment/equipment-client.html',{
           "all_Equip":equips,
           "client_count":client_count
       })

路由

path('clientlist/', ClientListView.as_view(), name='client_list'),

前端

模糊搜索

<input class="layui-input" placeholder="开始日" name="start" id="start" lay-verify="date">
<input class="layui-input" placeholder="截止日" name="end" id="end" lay-verify="date">
<input type="text" id="search_keywords" name="keywords"  placeholder="这里搜索关键词" autocomplete="off" class="layui-input" value="">
<button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>

分页

<a class="prev" href="?page=1">首页</a>
            {% if all_Equip.has_previous %}
                <a class="prev" href="?{{ all_Equip.previous_page_number.querystring }}">上一页</a>
            {% endif %}
            {% for page in all_Equip.pages %}
                {% if page %}
                    {% ifequal page all_Equip.number %}
                        <span href="?{{ page.querystring }} " class="current">{{ page }}</span>
                    {%  else %}
                        <a href="?{{ page.querystring }}" class="num">{{ page }}</a>
                    {% endifequal %}
                    {% else %}
                    <a href="">...</a>
                {% endif %}
            {% endfor %}
          {% if all_Equip.has_next %}
              <a href="?{{ all_Equip.next_page_number.querystring }}">下一页</a>
          {% endif %}
        <a href="?page={{ all_Equip.paginator.num_pages}}">尾页</a>

批量删除

<script>
			//删除用户
            function deleteSelect() {                
            if(confirm("确定删除该用户")){                
                a.setAttribute("onclick",'');                
                return true;            
                }        
            return false;        
            }
            //批量选择checkbox
            function seltAll() {
                var chckBoxSign = document.getElementById("selectAll");       //ckb 全选/反选的选择框id
                var chckBox = document.getElementsByName("chckBox");    //所有的选择框其那么都是chckBox
                var num = chckBox.length;
                if (chckBoxSign.checked) {
                    for (var index = 0; index < num; index++) {
                        chckBox[index].checked = true;
                    }
                } else {
                    for (var index = 0; index < num; index++) {
                        chckBox[index].checked = false;
                    }
                }
            }
			function delAll() {
			    var checkBox = document.getElementsByName("checkBox");
                var num = checkBox.length;
                var ids = "";
                for (var index = 0; index < num; index++) {
                    if (checkBox[index].checked) {
                        ids += checkBox[index].value + ",";
                    }
                }
                if (ids != "") {
                    ids = ids.substring(0, ids.length - 1); //S 删除字符串最后一个字符的几种方法
                    ids = {
                        'ids': ids
                    }
                     $.ajaxSetup({
                    data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
                });
                    if (window.confirm("确定删除所选记录?")) {
                        $.ajax({
                            type: "post",
                            url: '{% url 'equipment:taskdelete_equip' %}', //要自行删除的action
                            data: ids,
                            dataType: 'json',
                            success: function (data) {
                                if (data["success"]) {
                                    alert("删除成功");
                                    window.location.reload(true);
                                }
                            },
                            error: function (data) {
                                alert("系统错误,删除失败");
                            }
                        });
                    }
                } else {
                    alert("请选择要删除的记录");
                }
            }
        </script>

删除

<a title="删除" href = "{% url 'equipment:equip_delete' equip.id %}" onClick="deleteSelect();return false">删除

上面第一个函数的作用是跳出一个提示框(是否删除)、
第二个是函数的作用是表格全选的作用,第三个函数的作用就是批量删除

单个删除函数的视图
class DeleteEquipView(View):
    def get(self, request, id):
        if id:
            equip_profile = EquipmentInfo.objects.get(id=id)
            equip_profile.delete()
            return HttpResponseRedirect("/equipment/list")
        else:
            return HttpResponse('{"status":"删除失败"}',content_type='application/json')
批量函数的视图
class TaskDeleteEquipView(View):
    def post(self,request):
        print(1)
        if request.is_ajax():
            ids = request.POST["ids"]
            deletesql = EquipmentInfo.objects.extra(where=['id in (' + ids + ')'])
            context = {
                "success": '删除成功',
                "error": '删除失败'
            }
            if deletesql.delete():
                # return render(request, "home.html", context=context)
                return HttpResponse(json.dumps({
                    "success": '删除成功'
                }))
            else:
                return HttpResponse(json.dumps({
                    "error": '删除失败'
                }))
删除的路由
path('delete/<int:id>',DeleteEquipView.as_view(),name='equip_delete'),
path('taskdelete/',TaskDeleteEquipView.as_view(),name='taskdelete_equip'),
批量删除的前端
<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
# 这个就随便放了
<input type="checkbox" id="selectAll" onclick="seltAll();">
# 这个是总按钮,全选
<input type="checkbox" class="i-checks" name="checkBox" value="{{ equip.id }}">
# 这个放在for循环里面,每一列的点击按钮
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值