urls.py
urlpatterns = [ url(r‘^customer/‘, views.customer, name=‘customer‘), ]
views.py
from .pagination import Pagination def customer(request): customer_obj = Customer.objects.all() try: page = int(request.GET.get(‘page‘, 1)) except Exception: page = 1 pag_obj = Pagination(page=page, page_count=customer_obj.count(), qd=request.GET.copy(), per_num=3, max_show=3) customer_obj = customer_obj[pag_obj.start:pag_obj.end] return render(request, ‘customer.html‘, locals())
分页器(pagination.py)
# -*- coding: utf-8 -*- # __author__ = "maple" from django.http.request import QueryDict class Pagination(object): def __init__(self, page, page_count, qd=None, per_num=10, max_show=11): """ 分页 :param page:当前的页码数 :param page_count: 分页的总数据 :param per_num: 每页的数据条数 :param max_show: 最大展示页码(一页展示的页码数) """ self.page = page self.per_num = per_num self.page_count = page_count self.max_show = max_show # 数据切片控制 # 每页几条数据 # per_num = 2 # 总数据 # self.page_count # 总共的页数 self.page_count, remainder = divmod(self.page_count, self.per_num) if remainder: self.page_count += 1 # page_nums = range(1, page_nums + 1) # 起始 """ 第几页 切片起始索引 切片终止索引 1 0 10 #customers[0:10] 2 10 20 """ # 切片起始索引 self.start = (self.page - 1) * self.per_num # 切片终止索引 self.end = self.page * self.per_num # 切片 # customers = customers[self.start:self.end] # 页码控制 # 展示的页码数 # max_show = 3 half_show = self.max_show // 2 # 起始页码 self.start_page = self.page - half_show # 终止页码 self.end_page = self.page + half_show # 如果总页面数小于每页最大展示的页码数 # 就返回所有的页码数 if self.page_count <= self.max_show: self.start_page = 1 self.end_page = self.page_count # 如果最后一个页码的数字大于总页码数 # 就将终止页码定位最后页码数 elif self.end_page > self.page_count: self.start_page = self.end_page - self.max_show self.end_page = self.page_count # 控制起始页码不能小于等于零 if self.start_page < 1: self.start_page = 1 # 当起始页码为1时,控制页码长度为最大展示页码数 self.end_page = self.max_show # 如果不传request.GET,则实例化一个可以修改的request.GET对象 if not qd: """ 记录跳转到分页(第二页)之前的界面的url全地址 """ qd = QueryDict(mutable=True) self.qd = qd @property def page_html(self): # self.html = [] # 分页标签拼接 # # 追加上一页标签到列表里 # # 如果page-1为零时,禁止点击 # if self.page == 1: # self.html.append( # f‘<nav aria-label="Page navigation"><ul class="pagination"><li class="disabled"></li>‘) # else: # self.html.append( # f‘<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?page={self.page - 1}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘) # # # 追加页码a标签 # for n in range(self.start_page, self.end_page + 1): # if n == self.page: # li = f‘<li class="active"><a href="?page={n}">{n}</a></li>‘ # else: # li = f‘<li><a href="?page={n}">{n}</a></li>‘ # self.html.append(li) # # # 追加下一页标签到列表里 # if self.page == self.page_count: # self.html.append( # f‘<li class="disabled" ></li></ul></nav>‘) # else: # self.html.append( # f‘<li><a href="?page={self.page + 1}" aria-label="Next"><span aria-hidden="true">»</span></a></li></ul></nav>‘) # return ‘‘.join(self.html) self.html = [] # 分页标签拼接 # 追加上一页标签到列表里 # 如果page-1为零时,禁止点击 if self.page == 1: self.html.append( f‘<nav aria-label="Page navigation"><ul class="pagination"><li class="disabled"><a aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘) else: self.qd[‘page‘] = self.page - 1 self.html.append( f‘<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?{self.qd.urlencode()}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘) # 追加页码a标签 for n in range(self.start_page, self.end_page + 1): self.qd[‘page‘] = n if n == self.page: li = f‘<li class="active"><a href="?{self.qd.urlencode()}">{n}</a></li>‘ else: li = f‘<li><a href="?{self.qd.urlencode()}">{n}</a></li>‘ self.html.append(li) # 追加下一页标签到列表里 if self.page == self.page_count: self.html.append( f‘<li class="disabled"><a aria-label="Next"><span aria-hidden="true">»</span></a></li></ul></nav>‘) else: self.qd[‘page‘] = self.page + 1 self.html.append( f‘<li><a href="?{self.qd.urlencode()}" aria-label="Next"><span aria-hidden="true">»</span></a></li></ul></nav>‘) return ‘‘.join(self.html)
模板中使用
{{ pag_obj.page_html|safe }}
注:分页器中拥有bootstrap框架的类选择器代码,使用前需先在模板中引入bootstrap
原文:https://www.cnblogs.com/kindvampire/p/12342484.html