6-crm项目-kingadmin,列表页---分页
展示客户列表页面--------分页
分页的逻辑
分页记得要带上条件,
########
class BaseAdmin(object):
...
list_per_page = 20 --->这是基类
...
class CustomerAdmin(BaseAdmin):
...
list_per_page = 5 ---->如果要改在这里改
...
django官网paginationg使用说明
对应的view
@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','')})
对应的html
<ul class="pagination"> {% if query_sets.has_previous %} <li class=""><a href="?page={{ query_sets.previous_page_number }}">上页</a></li> {% endif %} {# <li class="active"><a>{{ query_sets.number }}</a></li>#} {# {% for loop_counter in query_sets.paginator.page_range %}#} {# {% render_page_ele loop_counter query_sets filter_condtions%}#} {# {% endfor %}#} {% build_paginators query_sets filter_condtions previous_orderby search_text%} {% if query_sets.has_next %} <li class=""><a href="?page={{ query_sets.next_page_number }}">下页</a></li> {% endif %} {# <li class="disabled"><a href="#">«</a></li>#} {# <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>#} {# #} </ul>
对应的tag
@register.simple_tag def build_paginators(query_sets,filter_condtions,previous_orderby,search_text): '''返回整个分页元素''' page_btns = '' filters = '' for k,v in filter_condtions.items(): filters += "&%s=%s" %(k,v) added_dot_ele = False #---->这是页码多的时候是否要显示点... for page_num in query_sets.paginator.page_range: if page_num < 3 or page_num > query_sets.paginator.num_pages -2 \ or abs(query_sets.number - page_num) <= 2: #代表最前2页或最后2页 #abs判断前后1页 ele_class = "" if query_sets.number == page_num: added_dot_ele = False ele_class = "active" page_btns += '''<li class="%s"><a href="?page=%s%s&o=%s&_q=%s">%s</a></li>''' % ( ele_class, page_num, filters,previous_orderby, search_text,page_num) # elif abs(query_sets.number - page_num) <= 1: #判断前后1页 # ele_class = "" # if query_sets.number == page_num: # added_dot_ele = False # ele_class = "active" # page_btns += '''<li class="%s"><a href="?page=%s%s">%s</a></li>''' % ( # ele_class, page_num, filters, page_num) else: #显示... if added_dot_ele == False: #现在还没加... page_btns += '<li><a>...</a></li>' added_dot_ele = True return mark_safe(page_btns)
###################
技术改变命运