自己开发的python分页插件

2021-10-01 12:14

阅读:831

标签:closed   display   safe   list   body   char   creat   sel   style   效果图:      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 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 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 = ‘首页‘ % (self.url) 82 item.append(ele) 83 if self.curren_page_num == 1 : 84 ele = ‘上一页‘ 85 else: 86 ele = ‘上一页‘ % (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 = ‘%s‘ % (self.url,i,i) 91 else: 92 ele = ‘%s‘ % (self.url, i, i) 93 item.append(ele) 94 95 if self.curren_page_num == self.total_page_num: 96 ele = ‘下一页‘ 97 else: 98 ele = ‘下一页‘% (self.url,self.curren_page_num + 1) 99 item.append(ele) 100 101 ele = ‘尾页‘ % (self.url,self.total_page_num) 102 item.append(ele) 103 104 105 return ‘‘.join(item) {% load staticfiles %} Title {% for i in data %} {{ i.name }} - {{ i.age }} {% endfor %} {{ item.item_list | safe}} index.html    自己开发的python分页插件标签:closed   display   safe   list   body   char   creat   sel   style   原文地址:https://www.cnblogs.com/xieys-1993/p/11910638.html


评论


亲,登录后才可以留言!