标签: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 <= 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>
标签:closed display safe list body char creat sel style
原文地址:https://www.cnblogs.com/xieys-1993/p/11910638.html