#原始碼! /usr/bin/env python
#-*- coding: utf-8 -*-
#__author__ = "shuke"
#date: 2017/9/9
class
page(object):
def__init__(self, current_page, all_count, base_url, per_page=10, pager_page_count=11):
""":param current_page: 當前頁
:param all_count: 資料總條數
:param base_url: 分頁的url
:param per_page: 每頁顯示的資料條數
:param pager_page_count: 每頁顯示的頁碼數量
"""self.current_page =current_page
self.per_page =per_page
self.all_count =all_count
self.base_url =base_url
self.pager_page_count =pager_page_count
pager_count, b =divmod(all_count, per_page)
if b !=0:
pager_count += 1self.pager_count =pager_count
@property
defstart(self):
"""資料庫獲取值的起始索引位置
:return:
"""return (self.current_page - 1) *self.per_page
@property
defend(self):
"""資料庫獲取值的結束索引位置
:return:
"""return self.current_page *self.per_page
defpage_html(self):
"""渲染的html頁碼
:return:
"""#
頁碼 pager_page_count =self.pager_page_count
half_pager_page_count = int(pager_page_count / 2)
if self.pager_count
pager_start = 1pager_end =self.pager_count
else
:
#資料較多,頁碼超過11,最少110條
if self.current_page <=half_pager_page_count:
pager_start = 1pager_end =pager_page_count
else
:
if (self.current_page + half_pager_page_count) >self.pager_count:
pager_start = self.pager_count - pager_page_count + 1pager_end =self.pager_count
else
: pager_start = self.current_page -half_pager_page_count
pager_end = self.current_page +half_pager_page_count
page_list =
if self.current_page <= 1:
# prev = '«'
else
:
# prev = '
«' %\
(self.base_url, self.current_page - 1)
for i in range(pager_start, pager_end + 1):
if self.current_page ==i:
#tpl = '%s' % (self.base_url, i, i)
tpl = '
%s' %(self.base_url, i, i)
else
:
#tpl = '%s' % (self.base_url, i, i)
tpl = '
%s' %(self.base_url, i, i)
if self.current_page >=self.pager_count:
# nex = '»'
else
:
# nex = '
»' %\
(self.base_url, self.current_page + 1)
page_str = ""
.join(page_list)
return page_str
django 自定義分頁
django框架雖然帶有自己的分頁功能,但是樣式不好看,為了做更好看的分頁,需要自定義乙個分頁工具,話不多說擼起袖子就開幹。1.首先先建乙個python檔案叫pager.py 如下 class pagination object page url 翻頁url路由 object list 資料來源列表...
django自定義分頁
django框架雖然帶有自己的分頁功能,但是樣式不好看,為了做更好看的分頁,需要自定義乙個分頁工具,話不多說擼起袖子就開幹。1.首先先建乙個python檔案叫pager.py 如下 class pagination object page url 翻頁url路由 object list 資料來源列表...
django 自定義分頁功能
django自帶的分頁功能有侷限性,只能顯示所有頁碼,不能顯示當前頁的前五和後五頁的頁碼。所有需要自己寫。from django.shortcuts import render class mypaginator def init self,cur page,per page,total,show ...