自己开发的python分页插件
Posted Mr-谢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己开发的python分页插件相关的知识,希望对你有一定的参考价值。
效果图:
views.py文件
1 from django.shortcuts import render 2 from app01.pager import Pagination 3 # Create your views here. 4 5 user_list = [] 6 7 for i in range(1,666): 8 user = {\'name\':\'root%s\'% i ,\'age\':i} 9 user_list.append(user) 10 11 12 def index(request): 13 cur_page = request.GET.get(\'p\') 14 15 item = Pagination(len(user_list),cur_page,\'index.html\') 16 data = user_list[item.start:item.end] 17 return render(request,\'index.html\',{\'data\':data,\'item\':item})
自己开发的分页 pager.py文件
1 class Pagination: 2 def __init__(self,maxPageItems,currenPageNum,url,pageItemLs=20,maxPageNum=11): 3 \'\'\' 4 5 :param maxPageItems: 总共有多少条数据 --- page_items_max 6 :param currenPageNum: 当前页码 --- curren_page_num 7 :param pageItemLs: 一页显示多少条数据--- page_item_list 8 :param maxPageNum: 页面最多显示多少页码---max_page_num 9 :param url: 在哪个页面进行分页 --- url 10 \'\'\' 11 self.url = url 12 self.page_items_max = maxPageItems 13 self.page_item_list = pageItemLs 14 15 #如果最多显示页码大于总页数,那就把总页数赋值给最多显示页码 16 self.max_page_num = maxPageNum if self.total_page_num > maxPageNum else self.total_page_num 17 \'\'\' 18 如果传入的当前页码不是整数,当前页码就直接赋值为1, 19 如果传入的当前页码小于等于0 ,当前页码就直接赋值为1, 20 如果传入的当前页码大于数据能分出来的最大页码数,就把当前页码赋值为最大页码数 21 否则就把传入的当前页码赋值给当前页码 22 \'\'\' 23 try: 24 v = int(currenPageNum) 25 if v <= 0 : 26 self.curren_page_num = 1 27 elif v > self.total_page_num: 28 self.curren_page_num = self.total_page_num 29 else: 30 self.curren_page_num = v 31 except Exception as e: 32 self.curren_page_num = 1 33 34 35 @property 36 def total_page_num(self): 37 \'\'\' 38 计算总页数 39 :return: 40 \'\'\' 41 total,b = divmod(self.page_items_max,self.page_item_list) 42 total = total + 1 if b != 0 else total 43 return total 44 45 @property 46 def start(self): 47 \'\'\'计算数据切片的起始切片位置\'\'\' 48 return ( self.curren_page_num -1 ) * self.page_item_list 49 50 @property 51 def end(self): 52 \'\'\'计算数据切片的结束切片位置\'\'\' 53 return self.curren_page_num * self.page_item_list 54 55 56 def pagenum_range(self): 57 \'\'\' 58 动态生成页码 59 :return: 60 \'\'\' 61 #以显示页码数量的一半为临界点 62 page = self.max_page_num // 2 63 if self.curren_page_num <= page: 64 #如果当前页码小于临界点,页码的显示就是 1 - 最大能分出的页码数 65 return range(1,self.max_page_num+1) 66 67 #如果(当前页 + page) 要大于 总页码数量, 页码数就显示 (总页数 - 一页最多显示页码数 +1) - (总页数 + 1) 68 if (self.curren_page_num + page) > self.total_page_num : 69 return range(self.total_page_num - self.max_page_num + 1 ,self.total_page_num +1 ) 70 71 # 页码数就显示 (当前页码数 - page) - (当前页 + page + 1) 72 return range(self.curren_page_num - page,self.curren_page_num + page + 1) 73 74 def item_list(self): 75 \'\'\' 76 返回HTML代码 77 :return: 78 \'\'\' 79 80 item = [] 81 ele = \'<li><a href="%s?p=1">首页</a></li>\' % (self.url) 82 item.append(ele) 83 if self.curren_page_num == 1 : 84 ele = \'<li class="disabled"><a>上一页</a></li>\' 85 else: 86 ele = \'<li><a href="%s?p=%s">上一页</a></li>\' % (self.url,self.curren_page_num - 1) 87 item.append(ele) 88 for i in self.pagenum_range(): 89 if i == self.curren_page_num: 90 ele = \'<li class="active"><a href="%s?p=%s">%s</a></li>\' % (self.url,i,i) 91 else: 92 ele = \'<li><a href="%s?p=%s">%s</a></li>\' % (self.url, i, i) 93 item.append(ele) 94 95 if self.curren_page_num == self.total_page_num: 96 ele = \'<li class="disabled"><a>下一页</a></li>\' 97 else: 98 ele = \'<li><a href="%s?p=%s">下一页</a></li>\'% (self.url,self.curren_page_num + 1) 99 item.append(ele) 100 101 ele = \'<li><a href="%s?p=%s">尾页</a></li>\' % (self.url,self.total_page_num) 102 item.append(ele) 103 104 105 return \'\'.join(item)
<!DOCTYPE html> <html lang="en"> <head> {% load staticfiles %} <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static \'plugin/bootstrap-3.3.7-dist/css/bootstrap.css\' %}"> </head> <body> <ul> {% for i in data %} <li> {{ i.name }} - {{ i.age }}</li> {% endfor %} </ul> <nav aria-label="..."> <ul class="pagination"> {{ item.item_list | safe}} </ul> </nav> </body> </html>
12-20更新,添加直接生成数据库数据,不需要在前端写html标签了
class Pagination: def __init__(self,items,currenPageNum,url=None,pageItemLs=1,maxPageNum=11): \'\'\' :param maxPageItems: 总共有多少条数据 --- page_items_max :param currenPageNum: 当前页码 --- curren_page_num :param pageItemLs: 一页显示多少条数据--- page_item_list :param maxPageNum: 页面最多显示多少页码---max_page_num :param url: 在哪个页面进行分页 --- url \'\'\' self.url = url self.items = items self.page_items_max = items.count() self.page_item_list = pageItemLs #如果最多显示页码大于总页数,那就把总页数赋值给最多显示页码 self.max_page_num = maxPageNum if self.total_page_num > maxPageNum else self.total_page_num \'\'\' 如果传入的当前页码不是整数,当前页码就直接赋值为1, 如果传入的当前页码小于等于0 ,当前页码就直接赋值为1, 如果传入的当前页码大于数据能分出来的最大页码数,就把当前页码赋值为最大页码数 否则就把传入的当前页码赋值给当前页码 \'\'\' try: v = int(currenPageNum) if v <= 0 : self.curren_page_num = 1 elif v > self.total_page_num: self.curren_page_num = self.total_page_num else: self.curren_page_num = v except Exception as e: self.curren_page_num = 1 def get_item(self): \'\'\' 根据分页生成数据返回 :return: \'\'\' return self.items[self.start:self.end] @property def total_page_num(self): \'\'\' 计算总页数 :return: \'\'\' total,b = divmod(self.page_items_max,self.page_item_list) total = total + 1 if b != 0 else total return total @property def start(self): \'\'\'计算数据切片的起始切片位置\'\'\' return ( self.curren_page_num -1 ) * self.page_item_list @property def end(self): \'\'\'计算数据切片的结束切片位置\'\'\' return self.curren_page_num * self.page_item_list def pagenum_range(self): \'\'\' 动态生成页码 :return: \'\'\' #以显示页码数量的一半为临界点 page = self.max_page_num // 2 if self.curren_page_num <= page: #如果当前页码小于临界点,页码的显示就是 1 - 最大能分出的页码数 return range(1,self.max_page_num+1) #如果(当前页 + page) 要大于 总页码数量, 页码数就显示 (总页数 - 一页最多显示页码数 +1) - (总页数 + 1) if (self.curren_page_num + page) > self.total_page_num : return range(self.total_page_num - self.max_page_num + 1 ,self.total_page_num +1 ) # 页码数就显示 (当前页码数 - page) - (当前页 + page + 1) return range(self.curren_page_num - page,self.curren_page_num + page + 1) def item_list(self,type=\'http\'): \'\'\' 返回HTML代码 :return: \'\'\' if self.page_items_max: item = [\'<nav aria-label="..." ><ul class="pagination">\',] if type == \'http\': item.append( \'<li><a href="%s?p=1">首页</a></li>\' % (self.url)) if self.curren_page_num == 1: item.append(\'<li class="disabled"><a>上一页</a></li>\') else: item.append(\'<li><a href="%s?p=%s">上一页</a></li>\' % (self.url, self.curren_page_num - 1)) for i in self.pagenum_range(): if i == self.curren_page_num: item.append(\'<li class="active"><a href="%s?p=%s">%s</a></li>\' % (self.url, i, i)) else: item.append(\'<li><a href="%s?p=%s">%s</a></li>\' % (self.url, i, i)) if self.curren_page_num == self.total_page_num: item.append(\'<li class="disabled"><a>下一页</a></li>\') else: item.append(\'<li><a href="%s?p=%s">下一页</a></li>\' % (self.url, self.curren_page_num + 1)) item.append(\'<li><a href="%s?p=%s">尾页</a></li>\' % (self.url, self.total_page_num)) elif type == \'ajax\': item.append(\'<li><a pager=1>首页</a></li>\') if self.curren_page_num == 1: item.append(\'<li class="disabled"><a>上一页</a></li>\') else: item.append(\'<li><a pager=%s>上一页</a></li>\' % (self.curren_page_num - 1)) for i in self.pagenum_range(): if i == self.curren_page_num: item.append(\'<li class="active"><a pager=%s>%s</a></li>\' % ( i, i)) else: item.append(\'<li><a pager=%s>%s</a></li>\' % ( i, i)) if self.curren_page_num == self.total_page_num: item.append(\'<li class="disabled"><a>下一页</a></li>\') else: item.append(\'<li><a pager=%s>下一页</a></li>\' % (self.curren_page_num + 1)) item.append(\'<li><a pager=%s>尾页</a></li>\' % (self.total_page_num)) item.append(\' </ul></nav>\') return \'\'.join(item) else: return \'\'
12.21更新
class Pagination: def __init__(self,items,getargs=None,url=\'\',pageItemLs=1,maxPageNum=11): \'\'\' :param items: 数据库查询的数据 :param currenPageNum: 当前页码 --- curren_page_num :param pageItemLs: 一页显示多少条数据--- page_item_list :param maxPageNum: 页面最多显示多少页码---max_page_num :param url: 在哪个页面进行分页 --- url :param getargs: 保留url带有get参数 --- url \'\'\' self.url = url self.items = items self.page_items_max = items.count() self.page_item_list = pageItemLs self.curren_page_num = None #如果最多显示页码大于总页数,那就把总页数赋值给最多显示页码 self.max_page_num = maxPageNum if self.total_page_num > maxPageNum else self.total_page_num \'\'\' 如果传入的当前页码不是整数,当前页码就直接赋值为1, 如果传入的当前页码小于等于0 ,当前页码就直接赋值为1, 如果传入的当前页码大于数据能分出来的最大页码数,就把当前页码赋值为最大页码数 否则就把传入的当前页码赋值给当前页码 \'\'\' self.get_args(getargs) try: v = int(self.curren_page_num) if v <= 0 : self.curren_page_num = 1 elif v > self.total_page_num: self.curren_page_num = self.total_page_num else: self.curren_page_num = v except Exception as e: self.curren_page_num = 1 def get_args(self,getargs): result = \'\' for k,v in getargs.items(): if k != \'p\': if v: result += \'&%s=%s\' % (k,v) else: self.curren_page_num = v self.url_args = result def get_item(self): \'\'\' 根据分页生成数据返回 :return: \'\'\' return self.items[self.start:self.end] @property def total_page_num(self): \'\'\' 计算总页数 :return: \'\'\' total,b = divmod(self.page_items_max,self.page_item_list) total = total + 1 if b != 0 else total return total @property def start(self): \'\'\'计算数据切片的起始切片位置\'\'\' return ( self.curren_page_num -1 ) * self.page_item_list @property def end(self): \'\'\'计算数据切片的结束切片位置\'\'\' return self.curren_page_num * self.page_item_list def pagenum_range(self): \'\'\' 动态生成页码 :return: \'\'\' #以显示页码数量的一半为临界点 page = self.max_page_num // 2 if self.curren_page_num <= page: #如果当前页码小于临界点,页码的显示就是 1 - 最大能分出的页码数 return range(1,self.max_page_num+1) #如果(当前页 + page) 要大于 总页码数量, 页码数就显示 (总页数 - 一页最多显示页码数 +1) - (总页数 + 1) if (self.curren_page_num + page) > self.total_page_num : return range(self.total_page_num - self.max_page_num + 1 ,self.total_page_num +1 ) # 页码数就显示 (当前页码数 - page) - (当前页 + page + 1) return range(self.curren_page_num - page,self.curren_page_num + page + 1) def item_list(self,type=\'http\'): \'\'\' 返回HTML代码 :return: \'\'\' if self.page_items_max: item = [\'<nav aria-label="..." ><ul class="pagination">\',] if type == \'http\': item.append( \'<li><a href="%s?p=1%s">首页</a></li>\' % (self.url,self.url_args)) if self.curren_page_num == 1: item.append(\'<li class="disabled"><a>上一页</a></li>\') else: item.append(\'<li><a href="%s?p=%s%s">上一页</a></li>\' % (self.url, self.curren_page_num - 1,self.url_args)) for i in self.pagenum_range(): if i == self.curren_page_num: item.append(\'<li class="active"><a href="%s?p=%s%s">%s</a></li>\' % (self.url, i,self.url_args, i)) else: item.append(\'<li><a href="%s?p=%s%s">%s</a></li>\' % (self.url, i,self.url_args, i)) if self.curren_page_num == self.total_page_num: item.append(\'<li class="disabled"><a>下一页</a></li>\') else: item.append(\'<li><a href="%s?p=%s%s">下一页</a></li>\' % (self.url, self.curren_page_num + 1,self.url_args)) item.append(\'<li><a href="%s?p=%s%s">尾页</a></li>\' % (self.url, self.total_page_num,self.url_args)) elif type == \'ajax\': item.append(\'<li><a pager=1>首页</a></li>\') if self.curren_page_num == 1: item.append(\'<li class="disabled"><a>上一页</a></li>\') else: item.append(\'<li><a pager=%s>上一页</a></li>\' % (self.curren_page_num - 1)) for i in self.pagenum_range(): if i == self.curren_page_num: item.append(\'<li class="active"><a pager=%s>%s</a></li>\' % ( i, i)) else: item.append(\'<li><a pager=%s>%s</a></li>\' % ( i, i)) if self.curren_page_num == self.total_page_num: item.append(\'<li class="disabled"><a>下一页</a></li>\') else: item.append(\'<li><a pager=%s>下一页</a></li>\' % (self.curren_page_num + 1)) item.append(\'<li><a pager=%s>尾页</a></li>\' % (self.total_page_num)) item.append(\' </ul></nav>\') return \'\'.join(item) else: return \'\'
以上是关于自己开发的python分页插件的主要内容,如果未能解决你的问题,请参考以下文章