分页
1.简单分页
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^index.html$', views.index), ]
from django.shortcuts import render,HttpResponse,redirect USER_LIST = [] for i in range(1,999): temp = {'name':'root'+str(i),'age':i} USER_LIST.append(temp) def index(request): per_page_count = 10 current_page = request.GET.get('p') current_page = int(current_page) #p=1 索引 0,10 0-9 #p=2 索引10,20 10-19 start = (current_page-1) * per_page_count end = (current_page) * per_page_count data = USER_LIST[start:end] prev_page = current_page -1 next_page = current_page +1 return render(request,'index.html',{'user_list':data,'prev_page':prev_page,'next_page':next_page})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in user_list %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> <a href="/index.html?p={{ prev_page }}">上一页</a> <a href="/index.html?p={{ next_page }}">下一页</a> </body> </html>
展示效果:
2.django内置分页
传入:
- 所有数据
- 当前页
- 每页显示10条
- 最多页面11个
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^index1.html$', views.index1), ]
from django.shortcuts import render,HttpResponse,redirect from django.core.paginator import EmptyPage, Paginator,PageNotAnInteger USER_LIST = [] for i in range(1,999): temp = {'name':'root'+str(i),'age':i} USER_LIST.append(temp) #django内置分页 def index1(request): # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 current_page = request.GET.get('p') #paginator对象 paginator = Paginator(current_page,11,USER_LIST,10) #每页10条数据 try: posts = paginator.page(current_page) #age对象 # 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,'index1.html',{'posts':posts})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for row in posts.object_list %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {% include 'include/pager.html' %} </body> </html>
{% if posts.has_previous %} <a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a> {% else %} <a href="#">上一页</a> {% endif %} {% if posts.has_next %} <a href="/index1.html?p={{ posts.next_page_number }}">下一页</a> {% else %} <a href="#">下一页</a> {% endif %} <apan> {{ posts.number }}/{{ posts.paginator.num_pages }} </apan>
展示效果:
扩展内置分页:
from django.shortcuts import render,HttpResponse,redirect from django.core.paginator import EmptyPage, Paginator,PageNotAnInteger USER_LIST = [] for i in range(1,999): temp = {'name':'root'+str(i),'age':i} USER_LIST.append(temp) class CustomPaginator(Paginator): def __init__(self, current_page,per_page_number,*args, **kwargs): #当前页 self.current_page = int(current_page) #最多显示页码数11 self.per_page_number = int(per_page_number) super(CustomPaginator, self).__init__(*args, **kwargs) def page_num_range(self): # self.current_page 当前页 # self.per_page_number 最多显示页码数11 # self.num_pages 总页数 if self.num_pages < self.per_page_number: # 总页数<11 return range(1,self.num_pages+1) # 遍历(1,总页数+1) part = int(self.per_page_number/2) if self.current_page <= part: # 当前页 <= 5 return range(1,self.per_page_number+1) # 遍历(1,11+1) if (self.current_page+part) >= self.num_pages: #当前页+5 >=总页数 return range(self.num_pages-self.per_page_number,self.num_pages+1) # 遍历(总页数-11,总页数+1) return range(self.current_page-part,self.current_page+part+1) #django内置分页 def index1(request): # per_page: 每页显示条目数量 # count: 数据总个数 # num_pages:总页数 # page_range:总页数的索引范围,如: (1,10),(1,200) # page: page对象 current_page = request.GET.get('p') #paginator对象 paginator = CustomPaginator(current_page,11,USER_LIST,10) #每页10条数据 try: posts = paginator.page(current_page) #age对象 # 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,'index1.html',{'posts':posts})
{% if posts.has_previous %} <a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a> {% else %} <a href="#">上一页</a> {% endif %} {% for i in posts.paginator.page_num_range %} {% if i == posts.number %} <a style="font-size: 30px" href="/index1.html?p={{ i }}">{{ i }}</a> {% else %} <a href="/index1.html?p={{ i }}">{{ i }}</a> {% endif %} {% endfor %} {% if posts.has_next %} <a href="/index1.html?p={{ posts.next_page_number }}">下一页</a> {% else %} <a href="#">下一页</a> {% endif %} <apan> {{ posts.number }}/{{ posts.paginator.num_pages }} </apan>
效果展示:
3,自定制分页
传入:
- 所有数据个数
- 当前页
- 每页显示10条
- 最多页面11个
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^index2.html$', views.index2), ]
def index2(request): from app01.pager import Pagination # 传入: # - 所有数据的个数 # - 当前页 # - 每页显示30条 # - 最多页面7个 current_page = request.GET.get('p') page_obj = Pagination(666, current_page) data_list = USER_LIST[page_obj.start():page_obj.end()] return render(request, 'index2.html', {'data': data_list, 'page_obj': page_obj})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"/> </head> <body> <ul> {% for row in data %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {% for i in page_obj.page_num_range %} <a href="/index2.html?p={{ i}}">{{ i }}</a> {% endfor %} <hr/> <ul class="pagination pagination-sm"> {{ page_obj.page_str|safe }} </ul> <div style="height: 300px"></div> </body> </html>
# !/usr/bin/env python # -*- coding: utf-8 -*- # Author:daly class Pagination(object): def __init__(self,totalCount,currentPage,perPageItemNum=10,maxPageNum=7): self.total_count= totalCount #数据总个数 try: v = int(currentPage) #当前页 if v <= 0: v = 1 self.current_page = v except Exception as e: self.current_page = 1 self.per_page_item_num = perPageItemNum #每页显示个数 self.max_page_num = maxPageNum #最多显示页数 def start(self): return (self.current_page-1) * self.per_page_item_num def end(self): return self.current_page * self.per_page_item_num @property def num_pages(self): #总页数 a,b = divmod(self.total_count,self.per_page_item_num) if b == 0: return a return a + 1 def page_num_range(self): # self.current_page 当前页 # self.max_page_num 最多显示页码数 # self.num_pages 总页数 if self.num_pages < self.max_page_num: return range(1,self.num_pages+1) part = int(self.max_page_num/2) if self.current_page <= part: return range(1,self.max_page_num+1) if (self.current_page+part) >= self.num_pages: return range(self.num_pages-self.max_page_num,self.num_pages+1) return range(self.current_page-part,self.current_page+part+1) def page_str(self): page_list = [] first = "<li><a href='/index2.html?p=1'>首页</a></li>" page_list.append(first) if self.current_page == 1: perv = "<li><a href='#'>上一页</a></li>" else: perv = "<li><a href='/index2.html?p=%s'>上一页</a></li>" %(self.current_page-1,) page_list.append(perv) for i in self.page_num_range(): if i == self.current_page: temp = "<li class='active'><a href='/index2.html?p=%s'>%s</a></li>"%(i,i) else: temp = "<li><a href='/index2.html?p=%s'>%s</a></li>" % (i, i) page_list.append(temp) if self.current_page == self.num_pages: nex = "<li><a href='#'>下一页</a></li>" else: nex = "<li><a href='/index2.html?p=%s'>下一页</a></li>" % (self.current_page + 1,) page_list.append(nex) last = "<li><a href='/index2.html?p=%s'>尾页</a></li>"%(self.num_pages,) page_list.append(last) return ''.join(page_list)
展示效果: