分页类代码:
class Page(object): ‘‘‘ 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypage page_obj = mypage.Page(total_num, current_page, ‘publisher_list‘) publisher_list = data[page_obj.data_start:page_obj.data_end] page_html = page_obj.page_html() 为了显示效果,show_page_num最好使用奇数 ‘‘‘ def __init__(self, total_num, current_page, url_prefix, per_page=10, show_page_num=9): ‘‘‘ :param total_num: 数据总条数 :param current_page: 当前访问的页码 :param url_prefix: 分页代码里a标签的前缀 :param per_page: 每一页显示多少数据 :param show_page_num: 页面上最多显示多少个页码 ‘‘‘ self.total_num = total_num self.url_prefix = url_prefix self.per_page = per_page self.show_page_num = show_page_num # 根据传入的值计算当前页码左右放置多少个页码 self.half_show_page_num = self.show_page_num // 2 # 计算当前数据总共需要多少页码 total_page, more = divmod(self.total_num, self.per_page) # 如果有余数就把页码加1 if more: total_page += 1 self.total_page = total_page # 数据有效性校验 try: current_page = int(current_page) except Exception as e: current_page = 1 # 如果当前页码数大于总页码数,默认展示最后一页数据 if current_page > self.total_page: current_page = self.total_page # 如果当前页码小于1,默认展示第一页数据 if current_page < 1: current_page = 1 self.current_page = current_page # 计算页面上需要展示的页码范围 # 页码过小处理 if self.current_page - self.half_show_page_num <= 1: page_start = 1 page_end = show_page_num # 页码过大处理 elif self.current_page + self.half_show_page_num >= self.total_page: page_end = self.total_page page_start = self.total_page - self.show_page_num + 1 # 未超过正常范围 else: page_start = self.current_page - self.half_show_page_num page_end = self.current_page + self.half_show_page_num # 计算得到的最终页码数 self.page_start = page_start self.page_end = page_end # 如果计算得到的页码数比总共需要展示页码数多,则把页码结束指定为总页码数 if self.page_end > self.total_page: self.page_end = self.total_page @property def data_start(self): ‘‘‘ :return: 返回当前页面应该从哪里开始切数据 ‘‘‘ return (self.current_page - 1) * self.per_page @property def data_end(self): ‘‘‘ :return: 返回当前页面从哪里结束数据 ‘‘‘ return self.current_page * self.per_page def page_html(self): li_list = [] # 添加前面的nav和ul标签 li_list.append(""" <nav aria-label="Page navigation"> <ul class="pagination"> """) # 添加首页 li_list.append(‘<li><a href="/{}/?page=1">首页</a></li>‘.format(self.url_prefix)) # 添加上一页 if self.current_page <= 1: # 没有上一页 prev_html = ‘<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘ else: prev_html = ‘<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>‘.format( self.url_prefix, self.current_page - 1) li_list.append(prev_html) # 循环添加内部所有的页码 for i in range(self.page_start, self.page_end + 1): if i == self.current_page: tmp = ‘<li class="active"><a href="/{0}/?page={1}">{1}</a></li>‘.format(self.url_prefix, i) else: tmp = ‘<li><a href="/{0}/?page={1}">{1}</a></li>‘.format(self.url_prefix, i) li_list.append(tmp) # 添加下一页 if self.current_page >= self.total_page: # 表示没有下一页 next_html = ‘<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">»</span></a></li>‘ else: next_html = ‘<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">»</span></a></li>‘.format( self.url_prefix, self.current_page + 1) li_list.append(next_html) # 添加尾页 li_list.append(‘<li><a href="/{}/?page={}">尾页</a></li>‘.format(self.url_prefix, self.total_page)) # 添加结尾nav和ul标签 li_list.append(""" </ul> </nav> """) # 将生成的标签拼接成一个大字符串 page_html = ‘‘.join(li_list) return page_html 导入文件后调用代码:
# 获取分页所需数据total_num = data.count() # 获取数据总计条数current_page = request.GET.get(‘page‘) # 获取当前页页码url_prefix = request.path_info.strip(‘/‘) # 获取a标签所需参数
# 调用分页类# 调用类传入参数生成实例page_obj = mypage.Page(total_num, current_page, url_prefix, per_page=1)# 按照分页所需数据对总数据进行分割获取当前页面所需展示的数据data = data[page_obj.data_start:page_obj.data_end]# 获取HTML代码page_html = page_obj.page_html()
原文地址:https://www.cnblogs.com/zhaodafa/p/9275491.html
时间: 2024-10-23 10:44:02