复制代码 使用就行
# coding: utf-8
# +-------------------------------------------------------------------
# | Project : ITSource
# | Author : 晴天雨露
# | QQ/Email : 184566608<qingyueheji@qq.com>
# | Time : 2019/10/9 2:36
# | Describe : Django分页插件
# +-------------------------------------------------------------------
from urllib import parse
class Pagination:
def __init__(self, request, totalCount, limit=20, showBtn=True, showLines=True):
'''
page = Pagination(request, sql_count)
[...][page.start:p.end]
html {{ page.page_html|safe }}
head->style {{ page.style|safe }}
python Django分页插件
:param request: {'page':1, 'lines': 20, 'btn': 11, 'path':get_full_path()} Django 默认参数
:param totalCount: 数据总量
:param limit: 查询数据数量
:param showBtn: 是否在url地址栏显示分页按钮参数
:param showLines: 是否在url地址显示要查询的数据条数
'''
self.__url = request.get_full_path() # 非 Django 框架要改改
self.__isbtn = showBtn
self.__islines = showLines
# 数据总页数
self.total = totalCount
# 当前页
try:
v = int(request.GET.get('page', 1))
if v <= 0:
v = 1
self.__current_page = v
except Exception as e:
self.__current_page = 1
# 每页显示的行数
try:
num_page = int(request.GET.get('limit', limit))
if num_page <= 0:
num_page = 1
self.__per_page_item_num = num_page
except:
self.__per_page_item_num = 1
# 分页按钮个数
try:
btn_num = int(request.GET.get('btn', 11))
if btn_num >= 15:
btn_num = 15
elif btn_num <= 0:
btn_num = 1
self.__btn_num = btn_num
except:
self.__btn_num = 11
@property # 方法伪造属性
def __num_pages(self):
'''总页数'''
a, b = divmod(self.total, self.__per_page_item_num)
if b == 0:
return a
return a + 1
@property
def __pager_num_range(self):
'''分页展示'''
if self.__num_pages < self.__btn_num:
return range(1, self.__num_pages + 1)
part = int(self.__btn_num / 2)
if self.__current_page <= part:
return range(1, self.__btn_num + 1)
if (self.__current_page + part) > self.__num_pages:
return range(self.__num_pages - self.__btn_num + 1, self.__num_pages + 1)
return range(self.__current_page - part, self.__current_page + part + 1)
@property
def __path(self):
'''拼装url_path携带的参数'''
url_split = parse.unquote(self.__url).split('?')
if len(url_split) > 1:
param_list = url_split[1].split('&')
param_dict = {}
for item in param_list:
temp = item.split('=')
if temp[0] in ['page', 'lines', 'btn']:
continue
param_dict[temp[0]] = temp[1]
if param_dict:
return ''.join((url_split[0], '?', parse.urlencode(param_dict), '&'))
return ''.join((url_split[0], '?'))
def __li_span(self, _class, text):
'''li>span html标签处理'''
status = {True: 'active', False: 'disabled'}
_class = status.get(_class)
return '<li class="%s"><span>%s</span></li>' % (_class, text)
def __li_a(self, text, page=0):
'''li>a html标签处理'''
param = self.__path
btn_num = self.__btn_num
item_num = self.__per_page_item_num
lia = '<li><a href="{path}page={page}&lines={lines}&btn={btn}">{text}</a></li>'
lia = lia.format(path=param, page=page, lines=item_num, btn=btn_num, text=text)
if not self.__islines:
lia = '<li><a href="{path}page={page}&btn={btn}">{text}</a></li>'
lia = lia.format(path=param, page=page, btn=btn_num, text=text)
if not self.__isbtn:
lia = '<li><a href="{path}page={page}&lines={lines}">{text}</a></li>'
lia = lia.format(path=param, page=page, lines=item_num, text=text)
if not self.__islines and not self.__isbtn:
lia = '<li><a href="{path}page={page}">{text}</a></li>'
lia = lia.format(path=param, page=page, text=text)
return lia
@property
def style(self):
return '.paginations{display:inline-block;padding-left:0;margin:10px auto 10px;border-radius:4px}.paginations>li{display:inline}.paginations>li>a,.paginations>li>span{position:relative;float:left;padding:6px 12px;margin-left:-1px;line-height:1.42857143;color:#337ab7;text-decoration:none;background-color:#fff;border:1px solid #ddd}.paginations>li:first-child>a,.paginations>li:first-child>span{margin-left:0;border-top-left-radius:4px;border-bottom-left-radius:4px}.paginations>li:last-child>a,.paginations>li:last-child>span{border-top-right-radius:4px;border-bottom-right-radius:4px}.paginations>li>a:focus,.paginations>li>a:hover,.paginations>li>span:focus,.paginations>li>span:hover{z-index:1;color:#23527c;background-color:#eee;border-color:#ddd}.paginations>.active>a,.paginations>.active>a:focus,.paginations>.active>a:hover,.paginations>.active>span,.paginations>.active>span:focus,.paginations>.active>span:hover{z-index:2;color:#fff;cursor:default;background-color:#337ab7;border-color:#337ab7}.paginations>.disabled>a,.paginations>.disabled>a:focus,.paginations>.disabled>a:hover,.paginations>.disabled>span,.paginations>.disabled>span:focus,.paginations>.disabled>span:hover{color:#777;cursor:not-allowed;background-color:#fff;border-color:#ddd}.paginations-sm>li>a,.paginations-sm>li>span{padding:5px 15px;font-size:16px;line-height:1.5}.paginations-sm>li:first-child>a,.paginations-sm>li:first-child>span{border-top-left-radius:3px;border-bottom-left-radius:3px}.paginations-sm>li:last-child>a,.paginations-sm>li:last-child>span{border-top-right-radius:3px;border-bottom-right-radius:3px}'
@property
def start(self):
'''开始页'''
return (self.__current_page - 1) * self.__per_page_item_num
@property
def end(self):
'''结束页'''
return self.__current_page * self.__per_page_item_num
@property
def page_html(self):
'''分页按钮展示'''
if self.total <= self.__per_page_item_num:
'''不够一页,返回空字符串'''
return ''
page_list = ['<ul class="paginations paginations-sm">']
if self.__current_page == 1:
first = self.__li_span(False, '首页')
prev = self.__li_span(False, '上一页')
else:
first = self.__li_a(text='首页', page=1)
prev = self.__li_a(text='上一页', page=self.__current_page - 1)
page_list.append(first)
page_list.append(prev)
for i in self.__pager_num_range:
if i == self.__current_page:
temp = self.__li_span(True, i)
else:
temp = self.__li_a(text=i, page=i)
page_list.append(temp)
if self.__current_page == self.__num_pages:
nex = self.__li_span(False, '下一页')
last = self.__li_span(False, '尾页')
else:
nex = self.__li_a(text='下一页', page=self.__current_page + 1)
last = self.__li_a(text='尾页', page=self.__num_pages)
page_list.append(nex)
page_list.append(last)
nav2 = '</ul>'
page_list.append(nav2)
return ''.join(page_list)