分页器在Django下写的,但是没有依赖任何Django的东西,可以在其他平台使用.
效果图如下:
使用之前先导入Bootstrap样式,提供下免费 CDN 加速服务
<!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 可选的 Bootstrap 主题文件(一般不用引入) --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
接下来都是分页器需要的文件:
urls.py文件
from django.conf.urls import re_path urlpatterns = [ re_path(r'^test$', views.test, name='test'), ]
views.py文件
# 导入数据库表,本案例需要的是models下面的Test from app01.models import Test # 导入分页器文件pager中的Pagination类 from customer.pager import Pagination def test(request): # 通过前台获取请求页面的page值 current_page = request.GET.get("page") # 通过数据库获取数据总条数 record_count = Test.objects.all().count() # 设置每页显示数据条数 page_record_limit = 5 # 设置显示的最多页面数 page_count_limit = 9 # 类Pagination实例化 page_obj = Pagination(current_page=current_page,record_count=record_count, page_record_limit=page_record_limit,page_count_limit=page_count_limit) # 数据库截取可客户请求的数据片段 data_list = Test.objects.all()[page_obj.start:page_obj.end] # 返回页面,page_obj是分页器的一个实例化对象,可以调用类里面的方法 return render(request, 'test.html', {'data': data_list, 'page_obj': page_obj})
创建test.html文件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> <div class="container"> <div class="page-header"> <h1>自定义分页器 <small> 阿波罗图书管理系统</small> </h1> </div> <div class="row"> <div class="col-md-6 col-md-offset-3"> <table class="table table-bordered table-hover table-striped"> <thead> <tr> <td>姓名</td> <td>年龄</td> </tr> </thead> {% for row in data %} <tr> <td>{{ row.name }}</td> <td>{{ row.age }}</td> </tr> {% endfor %} </table> </div> {# 分页样式 #} <div class="col-md-6 col-md-offset-3"> <ul class="pagination pagination-sm"> {{ page_obj.page_html|safe }} </ul> </div> </div> </div> </body> </html>
pager.py文件
#!/usr/bin/env python # -*- coding:utf-8 -*- class Pagination(object): # 封装分页相关数据 def __init__(self, current_page, record_count, page_record_limit, page_count_limit): """ :param page_count: 总页数 :param record_count: 数据总个数 :param page_range: 页的范围 :param page_count_limit: 最多显示页面 :param page_record_limit: 每页显示的行数 :param page_record_list: 页面数据列表 :param current_page: 当前页 :param page_mid: 页面显示页码中间位置页码 """ # 当前页码 try: current_page = int(current_page) if current_page <= 1: current_page = 1 except Exception as e: current_page = 1 self.current_page = current_page # 数据总个数 self.record_count = record_count # 每页显示的行数 self.page_record_limit = page_record_limit # 总页数(数据总个数record_count / 每页显示的行数page_record_limit) # [数据总个数]对[每页显示的行数]取余,如果余数为0,总页数等于商,否则等于商+1 page_count, tmp = divmod(record_count, page_record_limit) if tmp: page_count += 1 else: self.page_count = page_count # 页面页数显示限制(要奇数,便于对称显示) self.page_count_limit = page_count_limit # 每页第一条数据 @property def start(self): return (self.current_page - 1) * self.page_record_limit # 每页最后一条数据 @property def end(self): return self.current_page * self.page_record_limit def page_range(self): # 如果,总页码[小于]最多显示页面: if self.page_count <= self.page_count_limit: page_start = 1 page_end = self.page_count + 1 # 如果,总页码[大于]最多显示页面: else: # 页面显示页码中间位置页码 self.page_mid = int(self.page_count_limit-1)// 2 if self.current_page <= self.page_mid: page_start = 1 page_end = self.page_count_limit + 1 else: if (self.current_page + self.page_mid) > self.page_count: page_start = self.page_count - self.page_count_limit + 1 page_end = self.page_count + 1 else: page_start = self.current_page - self.page_mid page_end = self.current_page + self.page_mid + 1 return range(page_start, page_end) # 页面显示页码的控制 def page_html(self): # 准备页面渲染的数据 page_record_list = [] # 首页 first_page = '<li><a href="?page=%s">首页</a></li>' % (1,) page_record_list.append(first_page) # 处理上一页 if self.current_page <= 1: prev_page = '<li class="disabled"><a href="#">上一页</a></li>' else: prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,) page_record_list.append(prev_page) # 页码显示部分 for i in self.page_range(): if i == self.current_page: temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i) else: temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,) page_record_list.append(temp) # 处理下一页 if self.current_page >= self.page_count: next_page = '<li class="disabled"><a href="#">下一页</a></li>' else: next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,) page_record_list.append(next_page) # 尾页 last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.page_count,) page_record_list.append(last_page) return ''.join(page_record_list)