自定义分页组件
#########自定义分页模块########## """ 自定义分页组件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info) 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) params._mutable = True 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 = [] #首页 self.params['page'] = 1 first_page = '<a href="%s?%s">首页</a>'%(self.base_url,self.params.urlencode(),) page_html_list.append(first_page) #上一页 self.params['page'] = self.current_page-1 print(self.base_url) up_page = '<a href="%s?%s">上一页</a>'%(self.base_url,self.params.urlencode(),) page_html_list.append(up_page) 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) # 下一页 self.params['page'] = self.current_page + 1 print(self.base_url) up_page = '<a href="%s?%s">下一页</a>' % (self.base_url, self.params.urlencode(),) page_html_list.append(up_page) #尾页 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视图############# from django.shortcuts import render,redirect from pager import Pagination # Create your views here. HOST_LIST = [] for i in range(1,1464): HOST_LIST.append("ccc%s.com" %i) def hosts(request): 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() from django.http import QueryDict # request.GET是一个QueryDict类型, # 默认不可修改,request.GET._mutable = True # request.GET.urlencode() 用于讲k,v构造成URL格式字符串 # request.GET['page'] = 666 params = QueryDict(mutable=True) params['_list_filter'] = request.GET.urlencode() list_condition = params.urlencode() return render(request, 'hosts.html', {'host_list': host_list, "page_html": html,'list_condition':list_condition}) def edit(request,pk): if request.method == 'POST': url = "/host/?%s"%(request.GET.get('_list_filter')) return redirect(url) return render(request,'edit_host.html') # #当前页码 # current_page = int(request.GET.get('page')) # # #每页显示的数据条数 # per_page_count = 20 # # #计算数据 作为分割条件 # start = (current_page - 1) * per_page_count # end = current_page * per_page_count # host_list = HOST_LIST[start:end] # # #数据的总条数 # total_count = len(HOST_LIST) # # #页面应该显示的最大页码 # max_page_num,div=divmod(total_count,per_page_count) # if div: # max_page_num+=1 # page_html_list = [] # for i in range(1,max_page_num+1): # if i == current_page: # temp = '<a class="active" href="/host/?page=%s">%s</a>' % (i, i,) # else: # temp='<a href="/host/?page=%s">%s</a>'%(i,i,) # # page_html_list.append(temp) # page_list = ''.join(page_html_list) # return render(request,'hosts.html',{'host_list':host_list,'page_html':page_list}) #########HTML页面############### <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .pager a{ display: inline-block; padding: 2px 8px; margin: 0 5px; border: 1px solid cadetblue; } .pager a.active{ background-color: #2b669a; color: white; } </style> </head> <body> <ul> {% for item in host_list %} <li>{{ item }}<a href="/edit/{{ forloop.counter }}/?{{ list_condition }}">编辑</a></li> {% endfor %} </ul> <div class="pager"> {{ page_html|safe }} </div> </body> </html> ###########编辑页面########################## <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form method="post"> {% csrf_token %} <input type="text"> <input type="submit" value="提交"> </form> </body> </html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步