预备知识:
request.GET 我们得到的是一个QueryDict,然而这个QueryDict是不能改变的,只能看,如果我们想要改变的话,需要使用copy模块的copy.deepcopy,进行深拷贝
自己写一个page.py文件,把类写好
class Pagination(object): def __init__(self,current_page_num,all_count,request,per_page_num=2,pager_count=11): """ 封装分页相关数据 :param current_page_num: 当前访问页的数字 :param all_count: 分页数据中的数据总条数 :param per_page_num: 每页显示的数据条数 :param pager_count: 最多显示的页码个数 """ try: current_page_num = int(current_page_num) #判断访问页是否一个数字 except Exception as e: #如果访问页不是数字,就默认访问页为第一页 current_page_num = 1 if current_page_num <1: current_page_num = 1 self.current_page_num = current_page_num self.all_count = all_count self.per_page_num = per_page_num # 总数据/每页要显示的数据条数,得到一个元组 all_pager, tmp = divmod(all_count, per_page_num)#divmod返回一个元组,包含除法的结果和余数 if tmp: #如果有余数的话,总页数要+1 all_pager += 1 self.all_pager = all_pager self.pager_count = pager_count self.pager_count_half = int((pager_count - 1) / 2) # 实现左五右五的效果 # 保存搜索条件 import copy self.params=copy.deepcopy(request.GET) # {"a":"1","b":"2"},深拷贝QueryDict @property def start(self): #将start设为静态方法 return (self.current_page_num - 1) * self.per_page_num @property def end(self):#将end设为静态方法 return self.current_page_num * self.per_page_num def page_html(self): # 如果总页码 < 11个: if self.all_pager <= self.pager_count: pager_start = 1 #开始页码是第一页 pager_end = self.all_pager + 1 #结束页码是最后一页,+1是因为range顾头不顾尾 # 总页码 > 11 else: # 当前页如果<=页面上最多显示11/2个页码 if self.current_page_num <= self.pager_count_half: pager_start = 1 pager_end = self.pager_count + 1 # 当前页大于5 else: # 页码翻到最后 if (self.current_page_num + self.pager_count_half) > self.all_pager: pager_start = self.all_pager - self.pager_count + 1 pager_end = self.all_pager + 1 else: pager_start = self.current_page_num - self.pager_count_half pager_end = self.current_page_num + self.pager_count_half + 1 page_html_list = [] first_page = '<li><a href="?page=%s">首页</a></li>' % (1,) page_html_list.append(first_page) if self.current_page_num <= 1: prev_page = '<li class="disabled"><a href="#">上一页</a></li>' else: prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page_num - 1,) page_html_list.append(prev_page) #self.params=copy.deepcopy(request.GET) # {"a":"1","b":"2"} #保存访问记录 for i in range(pager_start, pager_end): self.params["page"]=i if i == self.current_page_num: #urlencode()可以再把修改后的querydict组包变成a=1&b=2的形式 temp = '<li class="active"><a href="?%s">%s</a></li>' %(self.params.urlencode(),i) else: temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(),i,) page_html_list.append(temp) if self.current_page_num >= self.all_pager: next_page = '<li class="disabled"><a href="#">下一页</a></li>' else: next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page_num + 1,) page_html_list.append(next_page) last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,) page_html_list.append(last_page) return ''.join(page_html_list)
视图部分:
def index(request): # 自定义分页 print(request.GET) from app01.page import Pagination #引入自己写的类 current_page_num = request.GET.get("page") #获取当前页 book_list = Book.objects.all() #所有书籍的列表 pagination=Pagination(current_page_num,book_list.count(),request) #实例化对象,把当前页,数据的总条数传给对象 ''' count=100 per_page=9 current_page_num=1 start 0 end 8 current_page_num=2 start 8 end 16 current_page_num=3 start 16 end 24 current_page_num=n start (n-1)*per_page end n*per_page ''' book_list=book_list[pagination.start:pagination.end] #这个地方的book_list指的是每一页显示的书籍 return render(request,"index.html",locals())
浙公网安备 33010602011771号