Django分页(一)
Django分页(一)
手动实现简单分页
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="header"> <div> {% for user in user_list %} <li>{{ user.name }} {{ user.age }} </li> {% endfor %} {{ prev_page }}<a href="/index?p={{ prev_page }}">上一页</a> {{ next_page }}<a href="/index?p={{ next_page }}">下一页</a> </div> </div> </body> </html>
Views代码
from django.shortcuts import render,HttpResponse,redirect from blog.models import * from django.views import View # Create your views here. USER_LIST=[] for i in range(888): temp={'name':'root'+str(i),'age':i} USER_LIST.append(temp) per_page_count = 10 # 定义每页要显示多少行数据 def get_index(request): current_page=request.GET.get('p')#获取客户端传过来的参数,得到翻到哪一页 current_page = int(current_page) # 字符--〉数字 start = (current_page - 1) * per_page_count # 定义从哪一行开始 end = current_page * per_page_count # 定义数据在哪一行结束 data = USER_LIST[start:end] # 对数据进行切片 # p=1 # 0,10 0-9 # p=2 # 10,20 10-19
#上一页 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})
Django分页简单使用
在html文件下创建include目录,然后把分页功能代码放入其中
使用include引入功能html文件
功能html文件如下:
{% 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 %}
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="header"> <div> <ul> {% for user in posts.object_list %} <li>{{ user.name }} {{ user.age }} </li> {% endfor %} </ul> {% 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 %} <!--{% include 'include/page1.html' %}--> </div> </div> </body> </html>
View代码
from django.shortcuts import render,HttpResponse,redirect from blog.models import * from django.views import View # Create your views here. USER_LIST=[] for i in range(888): temp={'name':'root'+str(i),'age':i} USER_LIST.append(temp) per_page_count = 10 # 定义每页要显示多少行数据 from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger def get_index1(request): #django分页主要有两个对象:paginator与page对象 #如果这里是表的话models.表名.objects.all()来操作 #将表里所有的内容都放进去,每页显示10条记录 paginator=Paginator(USER_LIST,10) #实例化Paginator #全部数据:USER_LIST #per_page:每页显示条目数量 #count:数据据总个数 #num_pages:总页数 #page_range:总页数的索引范围 #page:page对象(是否有上一页,是否有下一页) current_page=request.GET.get('p') current_page = int(current_page) # 字符--〉数字 print(current_page) try: posts = paginator.page(current_page) # has_next 是否有下一页 # next_page_number 下一页页码 # has_previous 是否有上一页 # previous_page_number 上一页页码 # object_list 分页之后的数据列表 # number 当前页 # paginator paginator对象 # print(posts.object_list) except PageNotAnInteger:#如果输入内容不为整数,传入页面1 posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request, 'index1.html', {'posts': posts})
Django分页自定义使用
功能html
{% 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 %}
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="header"> <div> <ul> {% for user in posts.object_list %} <li>{{ user.name }} {{ user.age }} </li> {% endfor %} </ul> {% include 'include/page1.html' %} </div> </div> </body> </html>
View代码
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) def get_index2(request): current_page = request.GET.get('p') current_page=int(current_page) paginator = CustomPaginator(current_page, 11, USER_LIST, 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对象 print(posts.object_list) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request, 'index2.html', {'posts': posts})