>백엔드 개발 >PHP 튜토리얼 >django 表单分页

django 表单分页

WBOY
WBOY원래의
2016-06-06 20:07:451043검색

<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>

点击到最后一页,效果如下:
django 表单分页

但实际上只有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>

点击到最后一页,效果如下:
django 表单分页

但实际上只有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>

可以再加一层判断,判断是否超出

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.