<code> {% ifnotequal cur_page 1 %} <li> <a href="/product/pdt">|<</a></li> <li> <a href="/product/pdt?page_id={{ cur_page|add:-1 }}&{{ url }}"><span><</span></a> </li> {% else %} <li><span style="color: #d1d1d1;">|<</span></li> <li><span style="color: #d1d1d1;"><</span></li> {% endifnotequal %} <li> <a href="/product/pdt?page_id={{ cur_page }}&{{ url }}">{{ cur_page }}</a> </li> {% ifnotequal cur_page total_page %} <li> <a href="/product/pdt?page_id={{ cur_page|add:1 }}&{{ url }}">{{ cur_page|add:1 }}</a> </li> <li> <a href="/product/pdt?page_id={{ cur_page|add:2 }}&{{ url }}">{{ cur_page|add:2 }}</a> </li> {% endifnotequal %} {% if next %} <li> <a href="/product/pdt?page_id={{ cur_page|add:3 }}&{{ url }}"><span>></a> </li> {% else %} <li><span style="color: #d1d1d1;">></span></li> {% endif %} {% ifnotequal cur_page total_page %} <li> <a href="/product/pdt?page_id=%7B%7B%20total_page%20%7D%7D&%7B%7B%20url%20%7D%7D"><span>>|</span></a> </li> {% else %} <li><span style="color: #d1d1d1">>|</span></li> {% endifnotequal %}</code>
点击到最后一页,效果如下:
但实际上只有9页,判断next(即当前页是否存在下一页)标签时 加 3 溢出 到10页。
1、要求显示的页码范围数为3,不足的为2或者1;
请教该如何处理?
<code> {% ifnotequal cur_page 1 %} <li> <a href="/product/pdt">|<</a></li> <li> <a href="/product/pdt?page_id={{ cur_page|add:-1 }}&{{ url }}"><span><</span></a> </li> {% else %} <li><span style="color: #d1d1d1;">|<</span></li> <li><span style="color: #d1d1d1;"><</span></li> {% endifnotequal %} <li> <a href="/product/pdt?page_id={{ cur_page }}&{{ url }}">{{ cur_page }}</a> </li> {% ifnotequal cur_page total_page %} <li> <a href="/product/pdt?page_id={{ cur_page|add:1 }}&{{ url }}">{{ cur_page|add:1 }}</a> </li> <li> <a href="/product/pdt?page_id={{ cur_page|add:2 }}&{{ url }}">{{ cur_page|add:2 }}</a> </li> {% endifnotequal %} {% if next %} <li> <a href="/product/pdt?page_id={{ cur_page|add:3 }}&{{ url }}"><span>></a> </li> {% else %} <li><span style="color: #d1d1d1;">></span></li> {% endif %} {% ifnotequal cur_page total_page %} <li> <a href="/product/pdt?page_id=%7B%7B%20total_page%20%7D%7D&%7B%7B%20url%20%7D%7D"><span>>|</span></a> </li> {% else %} <li><span style="color: #d1d1d1">>|</span></li> {% endifnotequal %}</code>
点击到最后一页,效果如下:
但实际上只有9页,判断next(即当前页是否存在下一页)标签时 加 3 溢出 到10页。
1、要求显示的页码范围数为3,不足的为2或者1;
请教该如何处理?
在Paginator上再封装一层Spagination ,Views调用
<code>obj = Model.object.filter(条件) pagin = Spagination.wraper(obj, pageSize, page) return pagin </code>
Spagination类代码
<code>#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 0: left_list.insert(0, self.page - p) for p in range(1,right_edge): if self.page + p 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</code>
template代码
<code><div class="pagerbox"> {%if pagin.has_prev %} <a href="javascript:void(0)" page="{{pagin.prev_num}}">上页</a> {%endif%} <a href="javascript:void(0)" page="1" pagin.page="=">首页</a> {%for p in pagin.visible_pages%} {%if p != 1 and p != pagin.pages%} <a href="javascript:void(0)" page="{{ p }}" pagin.page="=" p>{{p}}</a> {%endif%} {%endfor%} <a href="javascript:void(0)" page="{{ pagin.pages }}" pagin.page="=" pagin.pages>尾页</a> {%if pagin.has_next %} <a href="javascript:void(0)" page="{{pagin.next_num}}">下页</a> {%endif%} </div></code>
可以再加一层判断,判断是否超出