django -- 分页功能

自带分页功能 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def pageinator(request, Iterable, per_page, m):
    # django原生pageinator分页
    # Iterable 可迭代对象
    # per_page 每页多少个
    # m 小于等于这个数量的合并到上一页
    paginator = Paginator(Iterable, per_page, m)  

    page = request.GET.get('page')
    try:
        Iterable = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        Iterable = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        Iterable = paginator.page(paginator.num_pages)
    return Iterable

class MyOrderView(View):
    my_order = OrderMessage.objects.all()
    orders = orders = pageinator(request, my_order, 5, 3)
    return render(request, 'order.html', locals())
    <div class="pagination" style="text-align:center;display: block;">
        <span class="step-links">
            {% if orders.has_previous %}
                <a href="?page={{ orders.previous_page_number }}">上一页</a>
            {% endif %}

            {% for pg in orders.paginator.page_range %}
                {% if orders.number == pg %}
                    <span class="active"><a href="?page={{ pg }}">{{ pg }}</a></span>
                {% else %}
                    <span class=""><a href="?page={{ pg }}">{{ pg }}</a></span>
                {% endif %}
            {% endfor %}

            {% if orders.has_next %}
                <a href="?page={{ orders.next_page_number }}">下一页</a>
            {% endif %}
        </span>
    </div>

  • 第三方模块django-pure-pagination实现分页

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

def pure_page(request, objects, per_page=7):
    # django-pure-pagination模块
    p = Paginator(objects, per_page, request=request)
    try:
        page = request.GET.get('page', 1)
        if not page:
            page = 1
    except PageNotAnInteger:
        page = 1
    except EmptyPage:
        page = p.num_pages

    obj = p.page(page)
    return obj
    

class MyOrderView(View):
    my_order = OrderMessage.objects.all()
    orders = pure_page(request, orders, 10)
    return render(request, 'order.html', locals())

{% for m in orders.object_list %}
    {{ m.xxx }}
{% endfor %}


<!------ 分页 ------>
{% if orders %}
    <div class="pagination" style="text-align: center;display: block;">
    <span class="step-links">
        {% if orders.has_previous %}
            <a href="?{{ orders.previous_page_number.querystring }}" class="prev"
               style="margin: 0 10px;">上一页</a>
        {% endif %}

        {% for page in orders.pages %}
            {% if page %}
                {% ifequal page orders.number %}
                    <span class="active"><a href="javascript:void(0)"
                                            style="margin: 0 5px;display: inline-block;padding: 3px 9px;color: #fff;border: #aae 1px solid;background: #26b;">{{ page }}</a></span>
                {% else %}
                    <span><a href="?{{ page.querystring }}" class="page"
                             style="margin: 0 5px;display: inline-block;padding: 3px 9px;color: #15b;border: #aae 1px solid;">{{ page }}</a></span>
                {% endifequal %}
            {% else %}
                <span><a href="">...</a></span>
            {% endif %}
        {% endfor %}

        {% if orders.has_next %}
            <a href="?{{ orders.next_page_number.querystring }}" style="margin: 0 10px;">下一页</a>
        {% endif %}
    </span>
    </div>
{% else %}
    <div class="noMessage order_noMessage" style="display: block;">
        <p>没有查询到符合条件的记录!</p>
    </div>
{% endif %}
posted @ 2018-05-03 21:46  不专业的专业  阅读(393)  评论(0编辑  收藏  举报