1、排序是在过滤了之后进行排序的,在view.py视图里加上
#排序过后的数据
object_list,order_key = table_order(request,object_list)
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,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})
上面的table_order函数是在utils.py文件中:
先判断请求时是否有o这个字段,如果有就先给他排序,为了处理开始点击时是升序排序,再次去点击是降序排序(反转排序),会判断o这个字段请求过来的值(orderby_key)是否是以'-'开头,
如果是把"-"去掉,如果不是把"-"加上,最后返回排序后的值或者还是原来的值和o字段的值
order_by(a) #a=正的是升序,负的是降序 order_key.startswith("-") #判断是否一"-"开始的 order_key.strip("-") #去除“-”这个符号
def table_order(request,objs): """判断请求过来是否有o字段,有就排序""" orderby_key = request.GET.get("o") if orderby_key: objs=objs.order_by(orderby_key) if orderby_key.startswith("-"): orderby_key=orderby_key.strip("-") else: orderby_key = ("-%s")%orderby_key return objs,orderby_key
2、当o请求的时候,会把o也当成是一个筛选条件,导致排序不成功,处理方法是在请求有o这个字段时就不保存,代码如下在utils.py文件中:
def table_filter(request,admin_class): """进行条件过滤并返回过滤后的数据""" filter_conditions = {} for k,v in request.GET.items(): if k == 'page' or k == 'o': 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
3、在自定义标签tag.py文件中定义个函数返回页面值
3.1点击排序会有一个图标向上或者向下,可以在bootstrap里面直接复制过来如:
icon = '''<span class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>''' icon = '''<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>'''
可能这两个图标加载不出来,下载bootstrap的源码,把里面的内容拷到statics/fonts文件下
3.2 在tag.py文件里面定义一个函数判断返回前端页面的显示
3.2.1 判断请求的参数o(order_key)是否有值,如有再判断是否是以“-“开头的,则
icon = '''<span class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>'''
如果不是一"-"开头的则:
icon = '''<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>'''
在次判断如果请求过来的o()order_key)等于字段值就显示icon图标,如果不等于就不显示icon图标
如果请求过来的order_key是None,那么也是不显示icon图标
因为当在页面筛选时也能排序,所以在<a>标签的href里面拼接参数
代码如下:
@register.simple_tag def build_reverse_order(column,order_key,filter_condtions): """页面排序展示交换反转""" sel_ele = "" for k, v in filter_condtions.items(): sel_ele+= "&%s=%s" %(k, v) ele = '''<th><a href="?{sel_ele}&o={order_key}">{column}</a> {icon} </th> ''' if order_key: if order_key.startswith("-"): icon = '''<span class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>''' else: icon = '''<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>''' if order_key.strip("-") == column or order_key == column: order_key=order_key else: order_key=column icon = "" else: order_key = column icon="" ele = ele.format(order_key=order_key, column=column,icon=icon,sel_ele=sel_ele) return mark_safe(ele)
在返回到客户信息表重新点击进去的时候会报错,order_key是None,没有strip()这个属性,然后我在build_reverse_order函数加了一个order_key是否有值的判断,比视频早发现并改了有一点点的开心吧。嗯逻辑处理和思路还是得加强