1. 程式人生 > >django Paginator分頁外掛

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})