1、前端代码如下:
<div class="row"> <div class="col-lg-2"> <input type="search" name="_q" class="form-control" value="{{search_value}}" placeholder="search by {%for column in admin_class.search_fields %} {{column}} {% endfor %}" style="margin-left:15px"> </div> <div class="col-lg-2"> <button type="SUBMIT" class="btn btn-success">search</button> </div> </div>
前端搜索框里面的值当点击下一页时,里面的值会消失,在view.py文件中返回请求的数据值到前端value中,value="{{search_value}}"
def display_table_objs(request,app_name,table_name): print("-->",app_name,table_name) #models_module = importlib.import_module('%s.models'%(app_name)) #model_obj = getattr(models_module,table_name) admin_class = kind_admin.enabled_admins[app_name][table_name] #admin_class = king_admin.enabled_admins[crm][userprofile] #object_list = admin_class.model.objects.all() object_list,filter_condtions = table_filter(request,admin_class) #多条件搜索功能 object_list = table_search(request,admin_class,object_list) #排序过后的数据 object_list,order_key = table_order(request,object_list) paginator = Paginator(object_list, admin_class.list_per_page) # Show 25 contacts per page print("paginator------",paginator) page = request.GET.get('page') try: query_sets = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. query_sets = paginator.page(1) ##这个是对你的分页的数据进行取值 except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. query_sets = paginator.page(paginator.num_pages) print("query_sets------------",query_sets) return render(request,"kindadmin/table_objs.html",{"admin_class":admin_class, "query_sets":query_sets, "filter_condtions":filter_condtions, "order_key":order_key, "previous_orderkey":request.GET.get("o",""), "search_value":request.GET.get("_q","")})
当我点击下一页时,统计的数据不对,得把搜索的值也拼接加到分页的链接里面,上下页也是一样
前端加placeholder 告诉用户可以使用那几个字段搜索查询,值应该从admin_class里的search_fields循环取出
2、搜索查询也是在过滤后查询的,在view.py里增加:
object_list,filter_condtions = table_filter(request,admin_class) #多条件搜索功能 object_list = table_search(request,admin_class,object_list) #排序过后的数据 object_list,order_key = table_order(request,object_list)
table_order函数是在utils.py文件中:
获取请求过来的_q值,然后利用Q()去或查询,起初我没有判断search_value,导致我重新进入页面说None是不能够查询的:
#Q用法 from django.db.models import Q >>> from PerfectCRM.Perfectcrm.crm import models >>> from django.db.models import Q >>> models.Customer.objects.values('qq','name') <QuerySet [{'qq': '5676567@qq.com', 'name': '江大可'}, {'qq': '25064568@qq.com', 'name': '江可可'}, {'qq': '78787 4859@qq.com', 'name': '杨光'}, {'qq': '216564732@qqcom', 'name': '王大大'}, {'qq': '2787748877@qq.com', 'name': ' 刘新'}]> >>> con = Q() >>> con.connector="OR" >>> con.children.append(('qq__contains','5676567')) >>> con.children.append(('name__contains','江大')) #name包函的内容 >>> con <Q: (OR: ('qq__contains', '5676567'), ('name__contains', '江大'))> >>> models.Customer.objects.values('qq','name').filter(con) <QuerySet [{'qq': '5676567@qq.com', 'name': '江大可'}]> >>>
def table_search(request,admin_class,objs): """ 多条件搜索功能 :param request: 请求的 :param admin_class: Customer :param objs: 筛选过后的对象 :return: """ search_value = request.GET.get("_q","") if search_value: con = Q() con.connector="OR" for column in admin_class.search_fields: con.children.append(('%s__contains'%column,search_value)) ele = objs.filter(con) else: ele =objs return ele
提交的时候_q也会做为一个过滤条件,所以也会导致报错,解决是:
def table_filter(request,admin_class): """进行条件过滤并返回过滤后的数据""" filter_conditions = {} for k,v in request.GET.items(): kitem =['page','o','_q'] if k in kitem: continue if v: filter_conditions[k]=v print("filter_conditions:",filter_conditions) print("admin_class.model.objects.filter(**filter_conditions)",admin_class.model.objects.filter(**filter_conditions)) return admin_class.model.objects.filter(**filter_conditions),filter_conditions