django Paginator分页插件

 

 1 from django.shortcuts import render
 2 import importlib
 3 from king_admin.utils import table_filter
 4 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 5 # Create your views here.
 6 from king_admin import king_admin
 7 from crm import models
 8 def index(request):
 9 
10     return render(request, "king_admin/table_index.html",{'table_list':king_admin.enabled_admins})
11 
12 def display_table_objs(request,app_name,table_name):
13     # for i in range(50):
14     #     models.Customer.objects.create(qq="999888811100%s"%i,source=1,consult_course_id=1,content="了解一下",status="signed",consultant_id=1)
15     print(">>>",app_name,table_name)
16     # model_model = importlib.import_module('%s.models'%(app_name))
17     # model_obj=getattr(model_model,table_name)
18     admin_class=king_admin.enabled_admins[app_name][table_name]
19 
20     object_list,filter_condtions=table_filter(request,admin_class)#取到过滤后的字段和用字段查询出来的对象列表
21 
22     paginator=Paginator(object_list,admin_class.list_per_page)#第一个就是用于展示的数据(object_list),第二参数就是每页显示的大小(per_page)
23     # print('所有页面对象总数', paginator.count)
24     # print('页面总数',paginator.num_pages)
25     # print('页面范围',type(paginator.page_range),paginator.page_range)
26     # print('根据参数number返回一个Page对象', paginator.page(1))
27     # print('根据参数number返回一个Page对象', paginator.page(1).object_list)
28     # print('根据参数number返回一个Page对象', paginator.page(2).object_list)
29     # print('下一页判断', paginator.page(2).has_next())
30     # print('上一页判断', paginator.page(2).has_previous())
31     # print('其他页判断', paginator.page(2).has_other_pages())
32     # print('判断', paginator.page(2).previous_page_number())
33     # # print('判断', paginator.page(2).next_page_number())
34     # print('判断', paginator.page(2).start_index())
35     # print('判断', paginator.page(2).end_index())
36 
37 
38 
39     page=request.GET.get('page')#获取第几页
40     try:
41         query_sets=paginator.page(page)
42     except PageNotAnInteger:
43         query_sets=paginator.page(1)
44     except EmptyPage:
45         query_sets=paginator.page(paginator.num_pages)#Paginator.num_pages:总共分页数
46 
47     # print('根据参数number返回一个Page对象', query_sets.object_list)
48     # print('上一页判断', query_sets.has_previous())
49     # print('下一页判断', query_sets.has_next())
50     # print('当前页的序号,从1开始', query_sets.number)
51 
52 
53 
54 
55 
56     return render(request,"king_admin/table_objs.html",{"admin_class":admin_class,
57                                                         "query_sets": query_sets,
58                                                         "filter_condtions": filter_condtions})

 

posted @ 2019-01-04 12:30  巨兽~墨菲特  阅读(408)  评论(0编辑  收藏  举报