时间:2021-07-01 10:21:17 帮助过:43人阅读
{% ifnotequal cur_page 1 %}
|<
<
{% else %}
|<
<
{% endifnotequal %}
{{ cur_page }}
{% ifnotequal cur_page total_page %}
{{ cur_page|add:1 }}
{{ cur_page|add:2 }}
{% endifnotequal %}
{% if next %}
>
{% else %}
>
{% endif %}
{% ifnotequal cur_page total_page %}
>|
{% else %}
>|
{% endifnotequal %}
点击到最后一页,效果如下:
但实际上只有9页,判断next(即当前页是否存在下一页)标签时 加 3 溢出 到10页。
1、要求显示的页码范围数为3,不足的为2或者1;
请教该如何处理?
{% ifnotequal cur_page 1 %}
|<
<
{% else %}
|<
<
{% endifnotequal %}
{{ cur_page }}
{% ifnotequal cur_page total_page %}
{{ cur_page|add:1 }}
{{ cur_page|add:2 }}
{% endifnotequal %}
{% if next %}
>
{% else %}
>
{% endif %}
{% ifnotequal cur_page total_page %}
>|
{% else %}
>|
{% endifnotequal %}
点击到最后一页,效果如下:
但实际上只有9页,判断next(即当前页是否存在下一页)标签时 加 3 溢出 到10页。
1、要求显示的页码范围数为3,不足的为2或者1;
请教该如何处理?
在Paginator上再封装一层Spagination ,Views调用
obj = Model.object.filter(条件)
pagin = Spagination.wraper(obj, pageSize, page)
return pagin
Spagination类代码
#coding=utf-8
from django.core.paginator import Paginator
class Spagination(object):
def __init__(self, total, page=1, perpage=10):
self.total = total
self.pages = self.total / perpage + 1 if (self.total % perpage) > 0 else self.total / perpage
self.page = page
self.perpage = perpage
self.prev_num = page-1 if page > 1 else None
self.next_num = page+1 if page*perpage < total else None
self.has_prev = True if self.prev_num else False
self.has_next = True if self.next_num else False
self.visible_pages = self.iter_pages()
def prev(self):
if self.prev_num:
return Spagination(self.total, page=self.page-1, perpage=self.perpage)
raise 'There is no prev page!'
def next(self):
if self.next_num:
return Spagination(self.total, page=self.page+1, perpage=self.perpage)
raise 'There is no next page!'
def iter_pages(self, left_edge=5, right_edge=4, all=9):
left_list, right_list = [], []
for p in range(1,left_edge):
if self.page - p > 0:
left_list.insert(0, self.page - p)
for p in range(1,right_edge):
if self.page + p <= self.pages:
right_list.append(self.page + p)
if len(left_list) < left_edge and right_list:
for i in range(all-len(left_list)-len(right_list)-1):
if right_list[-1]+1 <= self.pages:
right_list.append(right_list[-1]+1)
if len(right_list) < right_edge and left_list:
for i in range(all-len(left_list)-len(right_list)-1):
if left_list[0]-1 > 0:
left_list.insert(0, left_list[0]-1)
return left_list+[self.page]+right_list
def setItems(self, items):
self.items = items
@classmethod
def wraper(cls, objs, limit, current_page):
paginator = Paginator(objs, limit)
items = paginator.page(current_page)
pagin = Spagination(paginator.count, current_page, limit)
pagin.setItems(items)
return pagin
template代码
{%if pagin.has_prev %}
上页
{%endif%}
首页
{%for p in pagin.visible_pages%}
{%if p != 1 and p != pagin.pages%}
{{p}}
{%endif%}
{%endfor%}
尾页
{%if pagin.has_next %}
下页
{%endif%}
可以再加一层判断,判断是否超出