1, a sorting to sort after the filtration, in view plus view.py
# Data after the sort
the object_list, order_key = the table_order (Request, the 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})
The above table_order function in utils.py file:
First determine whether there is a request o this field, if you give him the sort, in order to deal with is when to start clicking in ascending order, click again to sort in descending (reverse the sort), this field will determine o request over the value (orderby_key ) is whether - at the beginning, ''
If the "-" removed, if not the - plus value, and returns the sort or the original value or values of the field and o ""
ORDER_BY (A) # A = positive ascending, negative descending order_key.startswith ( " - " ) # determines whether a "-" Start of order_key.strip ( " - " ) # remove the "-" symbol
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, when the requested o, o will also function as a filter condition, causes the sequencer is unsuccessful, the processing method is not stored in the time of requesting the o field, as follows in utils.py file:
DEF table_filter (Request, admin_class): "" " data will be filtered and returned filtration conditions " "" 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, the definition of a function in a custom label tag.py return to the page file value
3.1 Click on an icon will sort up or down, you can copy the bootstrap directly over the inside such as:
icon = '''<span class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>''' icon = '''<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>'''
These two icons might be loaded out, download bootstrap source, the contents inside Kaodao statics / fonts file
3.2 tag.py define a function which determines a file front page display returns
3.2.1 determining parameters of the request o (order_key) has a value, and then determines whether the subject is "-" at the beginning, then
icon = '''<span class="glyphicon glyphicon-triangle-top" aria-hidden="true"></span>'''
If it is not a "-" at the beginning is:
icon = '''<span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>'''
In o () order_key) If the request is determined over the time field value is equal icon icon is displayed, if the icon is not displayed is not equal to icon
If the request is coming order_key None, so it is not displayed icon icon
Because when screening pages can be sorted, so the href <a> label inside the stitching parameters
code show as below:
@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)
Being given in return to the customer information table re-click into the time, order_key is None, no strip () this property, then I added a order_key has a value judgment in build_reverse_order function, early detection and change than the video of a little the happy now. Ah logic and ideas still have to strengthen