#!/usr/bin/env python
from django.utils.html import mark_safe
class
paginator
(object):
''' @:obj_all ---userinfo.objects.all()
@:per_page_count ---每頁顯示數量
@:show_page_count ---顯示頁碼的數量
@:base_url ---url字首
'''def__init__
(self, obj_all, base_url, per_page_count=10, show_page_count=11):
self.per_page_count = per_page_count
self.show_page_count = show_page_count
self.obj_all = obj_all
self.obj_list=
self.page_html = ''
self.total_item_count = ''
self.base_url = base_url
defpage
(self, current_page):
start = (current_page - 1) * self.per_page_count # 當前頁 - 1 * 每頁數量 正好為上乙個結束位置,作為這一次的開始
end = current_page * self.per_page_count # 當前頁 * 每頁數量 為這一次結束
self.obj_list= self.obj_all[start:end]
self.total_item_count = self.obj_all.count()
max_page_num, b = divmod(self.total_item_count, self.per_page_count)
if b:
max_page_num += 1
page_list =
# 首頁
to_start = '首頁
' % self.base_url
if current_page == 1:
'else:
' % (self.base_url, current_page - 1)
# 開始頁碼結束頁碼計算
half_show_page_count = int(self.show_page_count / 2) # 顯示頁碼除以2,取整數,算出當前頁前後各顯示多少
if max_page_num < self.show_page_count: # 如果總頁碼小於顯示頁碼數量,page_end按總頁碼來計算
page_start = 1
page_end = max_page_num
else:
if current_page <= half_show_page_count: # 如果當前頁前面不夠要顯示的頁碼,page_start從1開始,結束為要顯示頁碼數量
page_start = 1
page_end = self.show_page_count
else:
if max_page_num - current_page <= half_show_page_count: # 如果當前頁後邊不夠要顯示的頁碼,結束為最後一頁,開始為最後一頁減去要顯示的頁碼數量+1
page_start = max_page_num - self.show_page_count + 1
page_end = max_page_num
else:
page_start = current_page - half_show_page_count # 頁碼足夠的時候,開始:當前頁減去前邊要顯示的,結束:當前頁加上後邊要顯示的
page_end = current_page + half_show_page_count
# 生成頁碼html
for page in range(page_start, page_end + 1):
if page == current_page: # 如果page為當前頁,active
tpl = '%s
' % (self.base_url, page, page)
else:
tpl = '%s
' % (self.base_url, page, page)
if current_page == max_page_num:
'else:
' % (self.base_url, current_page + 1)
# 尾頁
to_end = '尾頁
' % (self.base_url, max_page_num)
self.page_html = mark_safe(''.join(page_list))
if __name__ == "__main__":
page = paginator(userinfo.objects.all(), 5, 11)
page.page(int(request.get.get("page", 1)))
page.obj_list---資料
page.page_html ---頁碼樣式
page.total_item_count ---據總頁數
萬能的分頁類
支援任意型別的分頁樣式 to change this template,choose tools templates and open the template in the editor.分頁類 使用方式 page new page page init 1000,20 page setnotact...
萬能儲存過程分頁
引數說明 tblname 需要查詢的表名。如圖書表tblbooks 如果是多表 圖書表關聯圖書分類表 則寫成 tblbooks inner join tblbooktypes on tblbooks.bookid tblbooktypes.bookid fldname 主鍵欄位名 bookid li...
SQL萬能儲存過程分頁
alter proc dbo proc pager tablename varchar 50 表名 refieldsstr varchar 200 欄位名 全部欄位為 orderstring varchar 200 排序字段 必須 wherestring varchar 500 n 條件語句 不用加...