好久没来写东西那!今天写个自定义分页器给大家参考下吧
首先我们在自己创建的Django项目的app下新建一个utils文件夹,用来放我们的分页器组件
简单说下分页器实现原理:
1.利用ORM查询语句的限制数据条数来确定每页显示的数据
2.设置我们每页显示的数据条数以及每页显示的页码数(例如本次默认页码数是11)
3.判断要显示的数据条数的页码数是否大于我们设置的页码总数
4.如果大于,则判断是否是页码总数的前段还是后段,是否要翻页处理;比如我们设置的页码总数是11个,那么就是用当前页-1/2得到半数,最后根据半数的值判断是否翻页
5.先利用函数的方式实现,随后利用class来完成封装
我们在utils文件下创建有个pager.py的文件
""" 自定义分页组件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET) host_list = HOST_LIST[pager_obj.start:pager_obj.end] html = pager_obj.page_html() return render(request,'hosts.html',{'host_list':host_list,"page_html":html}) """ class Pagination(object): """ 自定义分页 """ def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): try: current_page = int(current_page) except Exception as e: current_page = 1 if current_page <=0: current_page = 1 self.current_page = current_page # 数据总条数 self.total_count = total_count # 每页显示10条数据 self.per_page_count = per_page_count # 页面上应该显示的最大页码 max_page_num, div = divmod(total_count, per_page_count) if div: max_page_num += 1 self.max_page_num = max_page_num # 页面上默认显示11个页面(当前页在中间) self.max_pager_count = max_pager_count self.half_max_pager_count = int((max_pager_count - 1) / 2) # URL前缀 self.base_url = base_url # request.GET import copy params = copy.deepcopy(params) # 设置request能修改取到的值 params._mutable = True # 包含当前列表页面所有的搜索条件 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # self.params[page] = 8 # self.params.urlencode() # source=2&status=2&gender=2&consultant=1&page=8 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" # href="%s?%s" %(self.base_url,self.params.urlencode()) self.params = params @property def start(self): return (self.current_page - 1) * self.per_page_count @property def end(self): return self.current_page * self.per_page_count def page_html(self): # 如果总页数 <= 11 if self.max_page_num <= self.max_pager_count: pager_start = 1 pager_end = self.max_page_num # 如果总页数 > 11 else: # 如果当前页 <= 5 if self.current_page <= self.half_max_pager_count: pager_start = 1 pager_end = self.max_pager_count else: # 当前页 + 5 > 总页码 if (self.current_page + self.half_max_pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.max_pager_count + 1 else: pager_start = self.current_page - self.half_max_pager_count pager_end = self.current_page + self.half_max_pager_count page_html_list = [] # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} self.params['page'] = 1 first_page = '<a href="%s?%s">首页</a>' % (self.base_url,self.params.urlencode(),) page_html_list.append(first_page) # 上一页 if self.current_page == 0: page_up = '<a href="%s?%s">上一页</a>' % (self.base_url,self.params.urlencode()) else: self.params['page'] = self.current_page - 1 page_up = '<a class="active" href="%s?%s">上一页</a>' % (self.base_url,self.params.urlencode()) page_html_list.append(page_up) for i in range(pager_start, pager_end + 1): self.params['page'] = i if i == self.current_page: temp = '<a class="active" href="%s?%s">%s</a>' % (self.base_url,self.params.urlencode(), i,) else: temp = '<a href="%s?%s">%s</a>' % (self.base_url,self.params.urlencode(), i,) page_html_list.append(temp) # 下一页 if self.current_page == self.max_page_num: page_down = '<a href="%s?%s">下一页</a>' % (self.base_url,self.params.urlencode()) else: self.params['page'] = self.current_page + 1 page_down = '<a class="active" href="%s?%s">下一页</a>' % (self.base_url,self.params.urlencode()) page_html_list.append(page_down) self.params['page'] = self.max_page_num last_page = '<a href="%s?%s">尾页</a>' % (self.base_url, self.params.urlencode(),) page_html_list.append(last_page) return ''.join(page_html_list)
然后我们在view.py视图中调用
def hosts(request): from .utils.pager import Pagination HOST_LIST = [] for i in range(1, 213): HOST_LIST.append("c%s.com" % i) pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET) HOST_LIST = HOST_LIST[pager_obj.start:pager_obj.end] page_html = pager_obj.page_html() return render(request,'hosts.html',locals())
这样就完成那!