8-crm项目-kingadmin,列表页---搜索
展示客户列表页面--------搜索
第一步:views
@login_required def display_table_objs(request,app_name,table_name): print("-->",app_name,table_name) # 这是通过url取到的, #models_module = importlib.import_module('%s.models'%(app_name)) #model_obj = getattr(models_module,table_name) admin_class = king_admin.enabled_admins[app_name][table_name] #admin_class = king_admin.enabled_admins[crm][userprofile] if request.method == "POST": #action 来了 print(request.POST) selected_ids = request.POST.get("selected_ids") action = request.POST.get("action") if selected_ids: selected_objs = admin_class.model.objects.filter(id__in=selected_ids.split(',')) else: raise KeyError("No object selected.") if hasattr(admin_class,action): action_func = getattr(admin_class,action) request._admin_action = action return action_func(admin_class,request,selected_objs) #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,orderby_key = table_sort(request, admin_class, object_list) #排序后的结果 print("orderby key ", orderby_key) paginator = Paginator(object_list, admin_class.list_per_page) # 分页 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) return render(request,"king_admin/table_objs.html",{"admin_class":admin_class, "query_sets":query_sets, "filter_condtions":filter_condtions, "orderby_key":orderby_key, "previous_orderby": request.GET.get("o",''), "search_text":request.GET.get('_q','')})
第二步:tag
不需要tag
直接把要搜索的内容传递到后端,然后后端根据这个去数据库查询就可以了
第三步:html
<div class="col-lg-3" > <input type="search" style="margin-left:15px" name="_q" class="form-control" value="{{ search_text }}" placeholder="search by {% for search_field in admin_class.search_fields %}{{ search_field }},{% endfor %} "> </div> <div class="col-lg-2" > <button type="SUBMIT" class="btn btn-success">search</button> </div>
技术改变命运