1、分页模板类:
from django.core.paginator import Paginator, Page # 导入django分页模块 class PageInfo(object): def __init__(self, current_page, all_count, base_url, per_page=10, show_page=11): """ :param current_page: 当前页 :param all_count: 总页数 :param base_url: 模板 :param per_page: 每页显示数据条数 :param show_page: 显示链接页个数 """ #若url错误,默认显示第一页(错误类型可能为:空页面编号,非整数型页面编号) try: self.current_page = int(current_page) except Exception as e: self.current_page = 1 #根据数据库信息条数得出总页数 a, b = divmod(all_count, per_page) if b: a += 1 self.all_page = a self.base_url = base_url self.per_page = per_page self.show_page = show_page #当前页起始数据id def start_data(self): return (self.current_page - 1) * self.per_page #当前页结束数据id def end_data(self): return self.current_page * self.per_page #动态生成前端html def pager(self): page_list = [] half = int((self.show_page - 1)/2) #如果:总页数 < show_page,默认显示页数范围为: 1~总页数 if self.all_page < self.show_page: start_page = 1 end_page = self.all_page + 1 #如果:总页数 > show_page else: #如果:current_page - half <= 0,默认显示页数范围为:1~show_page if self.current_page <= half: start_page = 1 end_page = self.show_page + 1 else: #如果:current_page + half >总页数,默认显示页数范围为:总页数 - show_page ~ 总页数 if self.current_page + half > self.all_page: end_page = self.all_page + 1 start_page = end_page - self.show_page else: start_page = self.current_page - half end_page = self.current_page + half + 1 #首页 first_page = "<li><a href='%s?page=%s'>首页</a></li>" %(self.base_url, 1) page_list.append(first_page) #上一页(若当前页等于第一页,则上一页无链接,否则链接为当前页减1) if self.current_page <= 1: prev_page = "<li><a href='#'>上一页</a></li>" else: prev_page = "<li><a href='%s?page=%s'>上一页</a></li>" %(self.base_url, self.current_page-1) page_list.append(prev_page) #动态生成中间页数链接 for i in range(start_page, end_page): if i == self.current_page: temp = "<li class='active'><a href='%s?page=%s'>%s</a></li>" %(self.base_url, i, i) else: temp = "<li><a href='%s?page=%s'>%s</a></li>" % (self.base_url, i, i) page_list.append(temp) #下一页(若当前页等于最后页,则下一页无链接,否则链接为当前页加1) if self.current_page >= self.all_page: next_page = "<li><a href='#'>下一页</a></li>" else: next_page = "<li><a href='%s?page=%s'>下一页</a></li>" %(self.base_url, self.current_page+1) page_list.append(next_page) #末页(若总页数只有一页,则无末页标签) if self.all_page > 1: last_page = "<li><a href='%s?page=%s'>末页</a></li>" % (self.base_url, self.all_page) page_list.append(last_page) #print(''.join(page_list)) return ''.join(page_list)
2、视图调用模板
#视图-分页 def pagelist(request): pagenum = request.GET.get('page') if pagenum == None: pagenum = 1 #获取数据集合 datalist = db.GetQueryPage("""select * from (select ROW_NUMBER() OVER (order by id desc )AS RowNumber,* from tUsers) as temp where RowNumber between 10*({0}-1) and 10*{0};SELECT COUNT(1) total FROM tUsers""".format(pagenum)) all_count = datalist.get("datapage")[0].get("total") # 获取要显示数据库的总数据条数 page_info = pager.PageInfo(request.GET.get('page'), all_count, 'pagelist.htm',) print(page_info.start_data()) #page_info.pager() # 利用分页对象获取当前页显示数据 return render(request, 'pagelist.html', {'page_info': page_info,'datalist':datalist})
3、模板绑定数据+分页:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>customers</title> {# 引入bootstrap样式#} <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <h1>分页组件</h1> <table class="table"> <tr class="table title"> <td>序号</td> <td>编号</td> <td>姓名</td> <td>年龄</td> </tr> {% for blog in datalist.datalist %} <tr> <td>{{blog.RowNumber}}</td> <td>{{blog.id}}</td> <td>{{blog.name}}</td> <td>{{blog.age}}</td> </tr> {% endfor %} </table> {#分页#} <nav aria-label="Page navigation"> <ul class="pagination"> {# 传入page_info.pager#} {{ page_info.pager|safe }} </ul> </nav> </body> </html>