标签:方法 hidden 显示 rand max set ini tmp ota
import copy class Pagination(): def __init__(self,request,current_page,all_data_num,each_page_data_num=10,max_page_num=11): #封装页面相关数据 ‘‘‘ current_page:当前页 data_num:数据总条数 each_page:每页数据条数 max_page_num:最大显示的页码数 page_num: 总页数 ‘‘‘ self.url_data= copy.deepcopy(request.GET) # page =1&a=1&b=2 try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page <1: current_page = 1 self.current_page =current_page self.all_data_num = all_data_num #100 self.each_page_data_num =each_page_data_num # self.max_page_num = max_page_num #总大显示的页码数 # 计算总页数 total_page_num,tmp = divmod(self.all_data_num,self.each_page_data_num) if tmp: total_page_num +=1 self.total_page_num = total_page_num #总页数 self.page_count_half = int((self.max_page_num-1)/2) @property def start(self): return (self.current_page-1)*self.each_page_data_num @property def end(self): return self.current_page*self.each_page_data_num def page_html(self): #总页码 < 最大显示的页码数 if self.total_page_num <self.max_page_num: page_start = 1 page_end =self.total_page_num+1 #总页码 > 最大显示的页码数 else: if self.current_page <=self.page_count_half: page_start = 1 page_end =self.max_page_num+1 elif (self.current_page+self.page_count_half)>self.total_page_num: page_start=self.total_page_num-self.max_page_num+1 page_end =self.total_page_num+1 else: page_start =self.current_page-self.page_count_half page_end =self.current_page+self.page_count_half+1 page_html_lst=[] #首页,上一页标签 self.url_data["page"]=1 frist_page =‘<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li>‘%(self.url_data.urlencode(),) page_html_lst.append(frist_page) self.url_data["page"] = self.current_page-1 if self.current_page<=1: prev_page =‘<li class="disabled"><a href="#">上一页</a></li>‘ else: prev_page = ‘<li><a href="?%s">上一页</a></li>‘%(self.url_data.urlencode(),) page_html_lst.append(prev_page) #每页显示的页码 for i in range(page_start,page_end): self.url_data["page"] = i if i == self.current_page: tmp =‘<li class="active"><a href="?%s">%s</a></li>‘ % (self.url_data.urlencode(), i,) else: tmp =‘<li><a href="?%s">%s</a></li>‘ % (self.url_data.urlencode(), i,) page_html_lst.append(tmp) self.url_data["page"] = self.current_page + 1 if self.current_page >=self.total_page_num: next_page = ‘<li class="disabled"><a href="#">下一页</a></li>‘ else: next_page = ‘<li><a href="?%s">下一页</a></li>‘ % (self.url_data.urlencode(),) page_html_lst.append(next_page) self.url_data["page"] =self.total_page_num last_page = ‘<li><a href="?%s">尾页</a></li></ul></nav>‘ % (self.url_data.urlencode(),) page_html_lst.append(last_page) return "".join(page_html_lst)
自定义分页器的使用:
该分页器依赖:bootstrapt 的样式
pagination = Pagination(参数)
1. 最关键的两个参数:
1.当前页的页码 当前页码 reuqest.GET.get("page")
2.以及需要分页的数据条数
2. 该分页器 能做的:
1. 将数据分好页,且 通过该类的两个属性 ,可以得到当前页的数据范围 总数据进行切片 [pagination.start: pagination.end],得到当前页的数据,可以通过循环展示的页面。
2. pagination.page_html() 该方法,实现页码展示效果
批量创建数据 book_list=[] for i in range(1,101): book=Book(name="book%s"%i,price=random.randint(100,900)) book_list.append(book) Book.objects.bulk_create(book_list)
#django 分页器的使用 #总数据 book_lst=Book.objects.all()
#创建分页器对象 paginator = Paginator(book_lst,10) #参数 : 总数据,每页需要放的数据条数
print("总数据条数:",paginator.count) # 200 print("总共分了多少页:",paginator.num_pages) # 20 print("页码范围:",paginator.page_range) # range(1,21) page =paginator.get_page(3) #取第 3 页的数据 # paginator.get_page(n) 拿到第 n 页 的数据 # for book in page: # print(book) print("是否有下一页",page.has_next()) # 第3 页 有下一页 返回的是布尔值 :True print("是否有上一页",page.has_previous()) # 第 3 页有上一页 返回的是布尔值 :True print("下一页的数字:",page.next_page_number()) # 取到第3页的下一页数字4 print("上一页的数字:",page.previous_page_number()) #取第3页的上一页 数字 2
paginator =Paginator(data_list,10)
1.设计思路:
先从地址栏 发请求 的思路 开始考虑问题 http://127.0.0.1:8000/index/?page=3
地址栏 通过一个 page=1 的键值对 ,通过发get的请求给后端服务器, 后端通过 current_page= request.GET.get("page") 得到需要看的page数
通过 page_data = paginator .get_page(current_page) ,然后前端通过循环page_data 展示客户需要看的那一页数据
2. 设计 显示页码
后端逻辑:
book_list = Book.objects.all() paginator = Paginator(book_list,2) current_page=int(request.GET.get("page",1)) page = paginator.get_page(current_page) # 如果页数十分多时,换另外一种显示方式 , if paginator.num_pages>11: #页面最多显示的页码数 11表示:需要显示的页面码总数 ,以及上一页,下一页,首页 ,尾页 if current_page-5<1: page_range=range(1,12) elif current_page+5>paginator.num_pages: page_range=range(paginator.num_pages-10,paginator.num_pages+1) else: page_range=range(current_page-5,current_page+6) else: page_range=paginator.page_range return render(request,"index.html",{"page":page,"page_range":page_range,"current_page":current_page})
模板HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <ul> {% for book in page %} <li>{{ book.title }}---{{ book.price }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> <ul class="pagination"> <li><a href="?page=1" aria-label="Previous"><span aria-hidden="true">首页</span></a></li> {% if page.has_previous %} <li><a href="?page={{ page.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li> {% else %} <li class="disabled"><a aria-label="Previous"><span aria-hidden="true">上一页</span></a></li> {% endif %} {% for num in page_range %} {% if num == current_page %} <li class="active"><a href="?page={{ num }}">{{ num }}</a></li> {% else %} <li><a href="?page={{ num }}">{{ num }}</a></li> {% endif %} {% endfor %} <li> <a href="#" > <span aria-hidden="true">下一页</span> </a> </li> <li> <a href="#" > <span aria-hidden="true">尾页</span> </a> </li> </ul> </nav> </body> </html>
标签:方法 hidden 显示 rand max set ini tmp ota
原文地址:https://www.cnblogs.com/knighterrant/p/10292020.html