欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

django -- 分页功能

程序员文章站 2022-07-24 20:41:35
自带分页功能 [x] [x] 前端页面 第三方模块 实现分页 [x] [x] , 不能直接用orders来迭代, 要用 ......

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

  • [x] views.py
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())
  • [x] 前端页面order.html
    <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实现分页

  • [x] views.py
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())
  • [x] order.html, 不能直接用orders来迭代, 要用orders.object_list
{% 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 %}