1. 浏览器手动访问: http://127.0.0.1:8000/host/?page=2
后端 Host_list = [] for i in range(1,100): Host_list.append("host%s"%i) def host(request): current_page = int(request.GET.get("page")) per_page_count = 10 start = (current_page -1) * per_page_count end = current_page * per_page_count host_list = Host_list[start:end] return render(request,"host_list.html",{"host_list":host_list}) 前端
-
{% for item in host_list %}
- {{ item }} {% endfor %}
2. 显示页码
def host(request): try: current_page = int(request.GET.get("page")) except Exception: current_page =1 per_page_count = 10 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 = '%s'%(i,i) else: temp = '%s' % (i, i) page_html_list.append(temp) page_html = "".join(page_html_list) return render(request,"host_list.html",{"host_list":host_list,"page_html":page_html})
"en"> "UTF-8">Title
-
{% for item in host_list %}
- {{ item }} {% endfor %}
class="pager">
{{ page_html|safe }}
3. 显示指定数量的页码
Host_list = [] for i in range(1,1000): Host_list.append("host%s"%i) def host(request): try: current_page = int(request.GET.get("page")) except Exception: current_page =1 per_page_count = 10 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 #页面上默认显示11个页面 max_page_count = 11 half_max_page_count = int((max_page_count-1)/2) page_start = current_page - half_max_page_count page_end = current_page + half_max_page_count page_html_list = [] for i in range(page_start,page_end+1): if i == current_page: temp = '%s'%(i,i) else: temp = '%s' % (i, i) page_html_list.append(temp) page_html = "".join(page_html_list) return render(request,"host_list.html",{"host_list":host_list,"page_html":page_html})
4. 解决极限的选择 开始页和结尾页
Host_list = [] for i in range(1,1000): Host_list.append("host%s"%i) def host(request): try: current_page = int(request.GET.get("page")) except Exception: current_page =1 per_page_count = 10 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 #页面上默认显示11个页面 page_count = 11 half_max_page_count = int((page_count-1)/2) #如果总页数小于显示的页数 if max_page_num < page_count: page_start = 1 page_end = max_page_num else: #最左边判断 if current_page <= half_max_page_count: page_start =1 page_end = page_count else: #最右边判断 if (current_page + half_max_page_count) >max_page_num: page_start = max_page_num - page_count + 1 page_end = max_page_num else: page_start = current_page - half_max_page_count page_end = current_page + half_max_page_count page_html_list = [] for i in range(page_start,page_end+1): if i == current_page: temp = '%s'%(i,i) else: temp = '%s' % (i, i) page_html_list.append(temp) page_html = "".join(page_html_list) return render(request,"host_list.html",{"host_list":host_list,"page_html":page_html})
5. 组件 分装为类
""" 自定义分页组件的使用方法: 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,per_page_count=10,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.pager_count = pager_count self.half_pager_count = int((pager_count - 1) / 2) # URL前缀 self.base_url = base_url @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.pager_count: pager_start = 1 pager_end = self.max_page_num # 如果总页数 > 11 else: # 如果当前页 <= 5 if self.current_page <= self.pager_count: pager_start = 1 pager_end = self.pager_count else: # 当前页 + 5 > 总页码 if (self.current_page + self.pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count pager_end = self.current_page + self.pager_count page_html_list = [] first_page = '首页' page_html_list.append(first_page) for i in range(pager_start, pager_end + 1): if i == self.current_page: temp = '%s' % (self.base_url,i, i,) else: temp = '%s' % (self.base_url,i, i,) page_html_list.append(temp) end_page = '尾页' % self.max_page_num page_html_list.append(end_page) return ''.join(page_html_list)
6. 保留原搜索条件
保留原搜索条件 1. 方式一: - 缺点: 列表页面跳到编辑页面除了加列表页面的参数外 还增加了编辑页面的参数 再次返回列表页面获取到的参数无法确认哪些时列表页面的参数 - 列表页面 - 获取当前url的参数request.GET.urlencode() - 增加到 编辑按钮URL的后面 - 增加到 增加按钮URL的后面 - 编辑 增加 页面提交 - POST提交时 获取原来列表页面传过来的条件request.GET.urlencode() - 拼接URL /host/?原来的条件 list页面: list_condition = request.GET.urlencode() {% for item in host_list %}
7. 之后调分页
current_page = request.GET.get('page', 1)
total_count = self.model_class.objects.all().count()
base_url = request.path_info
page_list = Pagination(current_page, total_count, base_url, request.GET, per_page_count=2, pager_count=5)
html = page_list.page_html()
data_list = self.model_class.objects.all()[page_list.start:page_list.end]
return render(request, 'stark/changelist.html',
{'data_list': data_list, 'head_list': head_list, " "page_html": html})
""" 自定义分页组件的使用方法: 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,pamams,per_page_count=10,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.pager_count = pager_count self.half_pager_count = int((pager_count - 1) / 2) # URL前缀 self.base_url = base_url import copy pamams = copy.deepcopy(pamams) pamams._mutable = True self.pamams = pamams @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.pager_count: pager_start = 1 pager_end = self.max_page_num # 如果总页数 > 11 else: # 如果当前页 <= 5 if self.current_page <= self.pager_count: pager_start = 1 pager_end = self.pager_count else: # 当前页 + 5 > 总页码 if (self.current_page + self.pager_count) > self.max_page_num: pager_end = self.max_page_num pager_start = self.max_page_num - self.pager_count + 1 else: pager_start = self.current_page - self.pager_count pager_end = self.current_page + self.pager_count page_html_list = [] self.pamams['page'] = 1 first_page = '首页 '%(self.base_url,self.pamams.urlencode()) page_html_list.append(first_page) for i in range(pager_start, pager_end + 1): self.pamams['page'] = i if i == self.current_page: temp = '%s ' % (self.base_url,self.pamams.urlencode(), i,) else: temp = '%s ' % (self.base_url,self.pamams.urlencode(), i,) page_html_list.append(temp) #尾页 self.pamams['page'] = self.max_page_num end_page = '尾页 ' % (self.base_url,self.pamams.urlencode(),) page_html_list.append(end_page) return ''.join(page_html_list)