django 函数和类实现分页案例
方法一: 模拟分页
from django.shortcuts import render,HttpResponse from app01 import models def hostnames(request): current_page = int(request.GET.get('page')) #获取当前页数 per_page = 10 #定义每页显示的页数 all_data = models.Host.objects.all().count() #统计数据库数据条数 all_page,b = divmod(all_data,per_page) #获取分多少页显示 if b != 0: all_page += 1 start_page = (current_page-1) * per_page #定义从哪儿开始 end_page = current_page * per_page #定义从哪儿结束 host_list = models.Host.objects.all()[start_page:end_page] 从数据获取展示的数据 page_count = 11 half_page_count = int(page_count /2) if all_page < page_count: page_start = 1 page_end = page_count else: if current_page <= half_page_count: page_start = 1 page_end = page_count else: if (current_page+5) > all_page: page_end = all_page page_start = all_page-page_count else: page_start = current_page -half_page_count page_end = current_page + half_page_count url_list = [] if current_page <= 1: prev = '<a href="#">上一页</a>' else: prev = '<a href="/hostnames/?page=%s">上一页</a>'%(current_page-1) url_list.append(prev) for i in range(page_start,page_end+1): if current_page == i: tp = '<a class="active href="/hostnames/?page=%s">%s</a>' % (i, i) else: tp = '<a href="/hostnames/?page=%s">%s</a>' %(i,i) url_list.append(tp) if current_page >= all_page: next = '<a href="#">下一页</a>' else: next = '<a href="/hostnames/?page=%s">下一页</a>'%(current_page+1) url_list.append(next) page_str = "".join(url_list) print(page_str) return render(request,'hostnames.html',{'host_list':host_list,'page_str':page_str})
模拟分页结合bootstrape设置样式
后端代码 # 分页 users = [ {'username':'ivy_{}'.format(i),'pwd':'xxx'} for i in range(1,406)] def user_list(request): try: page = int(request.GET.get('page',1)) if page <= 0: page = 1 except Exception: page = 1 #每页显示的数据条数 per_num = 10 #总数据量 all_count = len(users) #计算总页数 all_page,more = divmod(all_count,per_num) if more: all_page+=1 #最多显示的页码数 max_show = 11 half_show = max_show//2 if all_page < max_show: page_start = 1 page_end = all_page else: if page <= half_show: page_start =1 page_end = max_show elif page + half_show > all_page: page_start = all_page - 11 +1 page_end = all_page else: page_start = page - half_show page_end = page + half_show li_list = [] #上一页 if page ==1: li = '<li class="disabled"><a href="?page={}">上一页</a></li>'.format(page) li_list.append(li) else: li = '<li><a href="?page={}">上一页</a></li>'.format(page-1) li_list.append(li) for i in range(page_start,page_end+1): if i == page: li = '<li class="active"><a href="?page={}">{}</a></li>'.format(i,i) else: li = '<li><a href="?page={}">{}</a></li>'.format(i, i) li_list.append(li) # 下一页 if page == all_page: li = '<li class="disabled"><a href="?page={}">下一页</a></li>'.format(page) li_list.append(li) else: li = '<li><a href="?page={}">下一页</a></li>'.format(page + 1) li_list.append(li) page_html = ''.join(li_list) start = (page-1)*per_num end = page * per_num return render(request,'user_list.html',{'users':users[start:end],'page_html':page_html}) 模板文件代码: {% extends 'layout.html' %} {% block content %} <table class="table"> <thead> <tr> <th> 用户名 </th> <th> 密码 </th> </tr> </thead> <tbody> {% for user in users %} <tr> <td>{{ user.username }}</td> <td>{{ user.pwd }}</td> </tr> {% endfor %} </tbody> </table> <nav aria-label="Page navigation"> <ul class="pagination"> {{ page_html | safe }} <form action=""> <input type="number" name="page"> <button>跳转</button> </form> </ul> </nav> {% endblock %}
类封装和使用
封装代码 from django.utils.safestring import mark_safe class Pagination(): def __init__(self,page,all_count,per_num=10,max_show=11): try: self.page = int(page) if self.page <= 0: self.page = 1 except Exception: self.page = 1 # 每页显示的数据条数 per_num = per_num # 总数据量 all_count = all_count # 计算总页数 self.all_page, more = divmod(all_count, per_num) if more: self.all_page += 1 # 最多显示的页码数 max_show = max_show half_show = max_show // 2 if self.all_page < max_show: page_start = 1 page_end = self.all_page else: if self.page <= half_show: page_start = 1 page_end = max_show elif self.page + half_show > self.all_page: page_start = self.all_page - 11 + 1 page_end = self.all_page else: page_start =self.page - half_show page_end = self.page + half_show self.page_start = page_start self.page_end = page_end # 切片起始位置 self.start = (self.page - 1) * per_num # 切片结束位置 self.end = self.page * per_num @property def page_html(self): li_list = [] # 上一页 if self.page == 1: li = '<li class="disabled"><a href="?page={}">上一页</a></li>'.format(self.page) li_list.append(li) else: li = '<li><a href="?page={}">上一页</a></li>'.format(self.page - 1) li_list.append(li) for i in range(self.page_start, self.page_end + 1): if i == self.page: li = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i) else: li = '<li><a href="?page={}">{}</a></li>'.format(i, i) li_list.append(li) # 下一页 if self.page == self.all_page: li = '<li class="disabled"><a href="?page={}">下一页</a></li>'.format(self.page) li_list.append(li) else: li = '<li><a href="?page={}">下一页</a></li>'.format(self.page + 1) li_list.append(li) s = ' <nav aria-label="Page navigation"><ul class="pagination">{}</ul></nav>'.format(''.join(li_list)) return mark_safe(s) 调用代码 # 分页 users = [ {'username':'ivy_{}'.format(i),'pwd':'xxx'} for i in range(1,406)] from utils import pagination def user_list(request): page_obj = pagination.Pagination(request.GET.get('page'),len(users)) return render(request,'user_list.html', {'users':users[page_obj.start:page_obj.end],'page_html':page_obj.page_html}) 模板文件代码 {% extends 'layout.html' %} {% block content %} <table class="table"> <thead> <tr> <th> 用户名 </th> <th> 密码 </th> </tr> </thead> <tbody> {% for user in users %} <tr> <td>{{ user.username }}</td> <td>{{ user.pwd }}</td> </tr> {% endfor %} </tbody> </table> {{ page_html }} {% endblock %} {#跳转代码#} {# <form action="">#} {# <input type="number" name="page">#} {# <button>跳转</button>#} {##} {# </form>#}
We are down, but not beaten. tested but not defeated.