來源:互聯網
上載者:User
關鍵字
django
python
php
html
c
{% 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-8from django.core.paginator import Paginatorclass 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%}
可以再加一層判斷,判斷是否超出