python/Djangof分頁與自定義分頁
阿新 • • 發佈:2017-06-27
from r+ else active count() 返回 log 多少 pan
python/Djangof分頁與自定義分頁
Django分頁
1 ##============================================分頁======================================================= 2 # current_pagc = request.GET.get(‘page‘) 3 # from django.core.paginator import Paginator,Page 4 # # result = models.UserInfo.objects.all().count()5 # result = models.UserInfo.objects.all() 6 # paginator=Paginator(result,10) 7 # try: 8 # posts=paginator.page(current_pagc) 9 # except Exception as c: 10 # posts = paginator.page(1) 11 # return render(request,‘geting.html‘,{‘posts‘:posts})
1 ##============================================django自定義分頁不能對頁數進行限定============================2 3 # class PageInfo(object): 4 # 5 # def __init__(self,current_page,per_page,all_count,show_page=11): 6 # try: 7 # self.current_page = int(current_page) 8 # except Exception as e: 9 # self.current_page=1 10 # self.per_page=per_page 11 # self.all_count=all_count12 # a,b =divmod(all_count,per_page) 13 # if b: 14 # a = a+1 15 # self.all_pager = a 16 # self.show_page=show_page 17 # 18 # def start(self): 19 # return (self.current_page -1)* self.per_page 20 # 21 # def end(self): 22 # return self.current_page * self.per_page 23 # 24 # def pager(self): 25 # # v=‘<a href="/custom.html?page=1">1</a>‘ 26 # # return v 27 # page_list=[] 28 # half=(self.show_page-1)/2 29 # begin=self.current_page - half 30 # stop=self.current_page +half +1 31 # for i in range(int(begin),int(stop)): 32 # if self.current_page == i: 33 # temp="<a style=‘display:inline-block;padding:5px;margin:5px;background-color:red‘ href=‘/custom.html?page=%s‘>%s</a>"%(i,i,) 34 # else: 35 # temp = "<a style=‘display:inline-block;padding:5px;margin:5px;‘ href=‘/custom.html?page=%s‘>%s</a>" % ( 36 # i, i,) 37 # page_list.append(temp) 38 # 39 # return ‘‘.join(page_list) 40 # def custom(request): 41 # #表示用戶當前想要訪問的頁碼 42 # all_count = models.UserInfo.objects.all().count() 43 # #每頁顯示的個數 44 # page_info=PageInfo(request.GET.get(‘page‘),10,all_count) 45 # user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()] 46 # return render(request,‘custom.html‘,{‘user_list‘:user_list,‘page_info‘:page_info})
自定義Django分頁
1 from django.core.paginator import Paginator,Page 2 class PageInfo(object): 3 4 def __init__(self,current_page,per_page,all_count,show_page=11): 5 ##類的變量參數 6 try: 7 #捕捉異常 8 self.current_page = int(current_page) 9 #如果能正常轉換成整型就賦值給self.current_page 10 except Exception as e: 11 #不能正常轉換成整型就賦值成數字1 12 self.current_page=1 13 #設置多少條數據分為一個一頁 14 self.per_page=per_page 15 #獲取數據庫中所有的數據能分為多少頁 16 self.all_count=all_count 17 #進行分頁計算(數據庫總數/多少天數據進行分頁)能除開等到結果就像是(12,0)把12賦值給a,把0賦值給b 18 a,b =divmod(all_count,per_page) 19 #如果b為真 20 if b: 21 #a 等於a+1 22 a = a+1 23 #求出總共要分的頁數賦值給self.all_pager 24 self.all_pager = a 25 #把傳入的show_page值賦值給self.show_page 26 self.show_page=show_page 27 28 def start(self): 29 #定義函數 設定起始位 30 return (self.current_page -1)* self.per_page 31 #返回當前頁數減1 乘每頁多少數據 32 def end(self): 33 # 定義函數 設定停止位 34 return self.current_page * self.per_page 35 # 返回當前頁數減乘每頁多少數據 36 def pager(self): 37 #定義函數 38 # v=‘<a href="/custom.html?page=1">1</a>‘ 39 # return v 40 page_list=[] 41 #創建列表 42 half=(self.show_page-1)/2 43 #獲取頁碼的中間值 44 if self.all_pager < self.show_page: 45 #如果數據庫的數小於設定的頁碼數 46 begin=1 47 #起始位設定為1 48 stop=self.all_pager+1 49 #停止為設定為數據庫的數加上1(因為切片是顧頭不顧尾) 50 else: 51 #否則 52 if self.current_page <= half: 53 #如果當前頁碼小於等於頁碼中間值 54 begin=1 55 #起始位設定為1 56 stop=self.show_page+1 57 #停止位設定為頁碼數加1(因為切片是顧頭不顧尾) 58 else: 59 #否則 60 if self.current_page + half > self.all_pager: 61 #如果當前頁碼加上中間的頁碼大於數據庫的總頁碼數 62 begin=self.all_pager-10 63 #起始值設定為數據庫總數減10 64 stop=self.all_pager+1 65 #停止值設定為數據庫總數加1(因為切片是顧頭不顧尾) 66 else: 67 #否則 68 begin=self.current_page-half 69 #起始位設定為當前頁碼減中間頁碼 70 stop=self.current_page+half+1 71 #停止位設定為當前頁碼加中間頁碼加1(因為切片是顧頭不顧尾) 72 if self.current_page==self.all_pager: 73 up = "<li><a href=‘#‘>下一頁</a></li>" 74 else: 75 up = "<li><a href=‘/custom.html?page=%s‘>下一頁</a></li>" % (self.current_page+1) 76 page_list.append(up) 77 for i in range(int(begin),int(stop)): 78 #進行循環範圍是(起始位,停止位) 79 if self.current_page == i: 80 #如果當前頁碼等於循環的i時 81 temp="<li class=‘active‘><a href=‘/custom.html?page=%s‘>%s</a></li>"%(i,i,) 82 #自定義一個a標簽設置簡單樣式 並添加背景顏色作為當前頁碼標識 83 else: 84 #否則 85 temp = "<li><a href=‘/custom.html?page=%s‘>%s</a></li>" % ( 86 i, i,) 87 #自定義一個a標簽設置簡單樣式 88 page_list.append(temp) 89 #把每循環的a標簽都添加到page_list列表中 90 if self.current_page==1: 91 nex = "<li><a href=‘#‘>上一頁</a></li>" 92 else: 93 nex = "<li><a href=‘/custom.html?page=%s‘>上一頁</a></li>" % (self.current_page-1) 94 page_list.append(nex) 95 return ‘‘.join(page_list) 96 #把列擺中的內容進行拼接(因為返回的是字符串) 97 # def custom(request): 98 # #從數據庫中獲取所有數據 99 # all_count = models.UserInfo.objects.all().count() 100 # #實例化一個對象並進行傳值 101 # page_info=PageInfo(request.GET.get(‘page‘),10,all_count) 102 # #從數據庫中獲取數據設置起始和結束位置 103 # user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()] 104 # #返回到HTML標簽中並傳入參數 105 # return render(request,‘custom.html‘,{‘user_list‘:user_list,‘page_info‘:page_info}) 106 107 ##==================================================總結================================================================= 108 ‘‘‘ 109 1.獲取到數據中最大的頁碼數 110 2.設定一頁顯示多少數據 111 3.在用數據庫總數除以要顯示的內容數量(如果余數不等於零需要多添加一頁) 112 4.設定一頁中顯示多少頁碼按鈕 113 5.獲取一頁頁碼中中間的頁碼數 114 6.判斷數據庫的頁碼是不是小於設定一頁中顯示頁碼按鈕的數(如果小於就重寫停止值,不小於就往下判斷) 115 7.判斷當前頁碼數小於等於設定頁碼的中間值(重寫起始值為1) 116 8.當前頁碼值不小於等於頁碼的中間值(重寫起始值為中間值-5,結束值+6,因為切片是顧頭不顧尾) 117 9.判斷當前頁碼加上中間頁碼數是不是大於數據庫總數(大於重寫起始值為數據庫總數-10,結束值為數據庫總數+1,因為切片是顧頭不顧尾) 118 10.通過python向HTML頁面中傳一個函數名,在HTML中函數名.方法執行(不能加括號執行) 119 11.|safe 表明是安全參數可以正常渲染 120 12.判斷當前頁碼數是不是等於1(如果等於1不執行上一步按鈕) 121 13.判斷當前頁碼數是不是等於數據庫總數(如果等於不執行下一步按鈕) 122 ‘‘‘
python/Djangof分頁與自定義分頁