Django-自定义分页
Django 内置分页:
from django.shortcuts import render from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger L = [] for i in range(999): L.append(i) def index(request): current_page = request.GET.get('p') paginator = Paginator(L, 10) # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 try: posts = paginator.page(current_page) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表 # number 当前页 # paginator paginator对象 except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request, 'index.html', {'posts': posts}) views.py
对应 HTML
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> {% for item in posts %} <li>{{ item }}</li> {% endfor %} </ul> <div class="pagination"> <span class="step-links"> {% if posts.has_previous %} <a href="?p={{ posts.previous_page_number }}">Previous</a> {% endif %} <span class="current"> Page {{ posts.number }} of {{ posts.paginator.num_pages }}. </span> {% if posts.has_next %} <a href="?p={{ posts.next_page_number }}">Next</a> {% endif %} </span> </div> </body> </html>
扩展内置分页:
from django.shortcuts import render from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger class CustomPaginator(Paginator): def __init__(self, current_page, max_pager_num, *args, **kwargs): """ :param current_page: 当前页 :param max_pager_num:最多显示的页码个数 :param args: :param kwargs: :return: """ self.current_page = int(current_page) self.max_pager_num = max_pager_num super(CustomPaginator, self).__init__(*args, **kwargs) def page_num_range(self): # 当前页面 # self.current_page # 总页数 # self.num_pages # 最多显示的页码个数 # self.max_pager_num print(1) if self.num_pages < self.max_pager_num: return range(1, self.num_pages + 1) print(2) part = int(self.max_pager_num / 2) if self.current_page - part < 1: return range(1, self.max_pager_num + 1) print(3) if self.current_page + part > self.num_pages: return range(self.num_pages + 1 - self.max_pager_num, self.num_pages + 1) print(4) return range(self.current_page - part, self.current_page + part + 1) L = [] for i in range(999): L.append(i) def index(request): current_page = request.GET.get('p') paginator = CustomPaginator(current_page, 11, L, 10) # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 try: posts = paginator.page(current_page) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表 # number 当前页 # paginator paginator对象 except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request, 'index.html', {'posts': posts})
扩展 HTML
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> {% for item in posts %} <li>{{ item }}</li> {% endfor %} </ul> <div class="pagination"> <span class="step-links"> {% if posts.has_previous %} <a href="?p={{ posts.previous_page_number }}">Previous</a> {% endif %} {% for i in posts.paginator.page_num_range %} <a href="?p={{ i }}">{{ i }}</a> {% endfor %} {% if posts.has_next %} <a href="?p={{ posts.next_page_number }}">Next</a> {% endif %} </span> <span class="current"> Page {{ posts.number }} of {{ posts.paginator.num_pages }}. </span> </div> </body> </html>
自定义分页:
1、设定每页显示数据条数
2、用户输入页码(第一页、第二页...)
3、设定显示多少页号
4、获取当前数据总条数
5、根据设定显示多少页号和数据总条数计算出,总页数
6、根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置
7、在数据表中根据起始位置取值,页面上输出数据
8、输出分页html,如:[上一页][1][2][3][4][5][下一页]
自定义分页实例:
创建PageInfo类:
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Fade Zhao' '''自定义分页''' class PageInfo: def __init__(self,current_page,total_item,per_page,base_url): ''' :current_page: 跳转的页数 :total_item: 数据总数 :per_page: 每页数据的个数 ''' try: self.current_page = int(current_page) except Exception as e: self.current_page =1 self.per_page = per_page self.total_item = total_item self.base_url =base_url def start(self): return (self.current_page-1)*self.per_page def end(self): return self.current_page*self.per_page def TotalPage(self): a,b = divmod(self.total_item,self.per_page) if b: return a+1 else: return a def pager(self,show_page=10): totalPage = self.TotalPage() print('totalPage=',totalPage) # 别再想念我 我会受不了这样 begin = 0 end = 0 half = int(show_page/2) print('half=',type(half)) if totalPage < show_page: begin = 0 end = totalPage else: if self.current_page <= half: # 第一页 begin = 0 end = show_page else: if self.current_page > totalPage-half: begin=totalPage-show_page end = totalPage else: if self.current_page > half: begin = self.current_page -half end = self.current_page+half # 首页跳转 page_list =[] if self.current_page <= 1: first_Page = "<a href=''>首页</a>" else: first_Page = "<a href='%s%d'>首页</a>" % (self.base_url,1) page_list.append(first_Page) #上一页 if self.current_page<=1: prev_Page = "<a href=''>上一页</a>" else: prev_Page = "<a href='%s%d'>上一页</a>" % (self.base_url,self.current_page-1) page_list.append(prev_Page) # 中间 for i in range(begin+1,end+1): if i == self.current_page: temp = "<a href='%s%d' style='background-color:#009a61;'>%d</a>" % (self.base_url, i, i) else: temp = "<a href='%s%d'>%d</a>" % (self.base_url, i, i) page_list.append(temp) # 下一页 if self.current_page >= totalPage: next_Page = "<a href='#'>下一页</a>" else: next_Page ="<a href='%s%d'>下一页</a>" % (self.base_url,self.current_page+1) page_list.append(next_Page) result = ''.join(page_list) return result
实例化PageInfo:
from django.shortcuts import HttpResponse,reverse,render from tools.PageInformation import PageInfo def custom(request): b_url = reverse('pager') b_url = b_url+'?current_page=' current_page = request.GET.get('current_page') if not current_page: current_page = 1 user_count = models.userInfo.objects.all().count() page_Info = PageInfo(current_page,user_count,10,b_url) user_list = models.userInfo.objects.all()[page_Info.start():page_Info.end()] return render(request, 'custom.html', {'user_list': user_list,'pageInfo':page_Info})
自定义分页对应的HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>中华小当家</title> </head> <body> <ul> {% for item in user_list %} <li>{{ item.username }}</li> {% endfor %} {{ pageInfo.pager|safe }} </ul> </body> </html>
总结,分页时需要做三件事:
- 创建处理分页数据的类
- 根据分页数据获取数据
- 输出分页HTML,即:[上一页][1][2][3][4][5][下一页]