zoukankan      html  css  js  c++  java
  • 45)django-分页实现

    Django提供了一个新的类来帮助你管理分页数据,.它可以接收列表、元组或其它可迭代的对象。

    一:常用方法

    >>> from django.core.paginator import Paginator
    >>> objects = ['john', 'paul', 'george', 'ringo']
    >>> p = Paginator(objects, 2)
    
    >>> p.count
    4
    >>> p.num_pages
    2
    >>> type(p.page_range)
    <class 'range_iterator'>
    >>> p.page_range
    range(1, 3)
    
    >>> page1 = p.page(1)
    >>> page1
    <Page 1 of 2>
    >>> page1.object_list
    ['john', 'paul']
    
    >>> page2 = p.page(2)
    >>> page2.object_list
    ['george', 'ringo']
    >>> page2.has_next()
    False
    >>> page2.has_previous()
    True
    >>> page2.has_other_pages()
    True
    >>> page2.next_page_number()
    Traceback (most recent call last):
    ...
    EmptyPage: That page contains no results
    >>> page2.previous_page_number()
    1
    >>> page2.start_index() # The 1-based index of the first item on this page
    3
    >>> page2.end_index() # The 1-based index of the last item on this page
    4
    
    >>> p.page(0)
    Traceback (most recent call last):
    ...
    EmptyPage: That page number is less than 1
    >>> p.page(3)
    Traceback (most recent call last):
    ...
    EmptyPage: That page contains no results

    二:定义工具

      定义工具,可供多个页面使用。

      效果如下:

      1)定义方法:pages.py

     

    # -*- coding:utf-8 -*-
    __author__ = 'lixiang'
    
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    
    def page(request,post_objects,page_number=20):
        """
        实现分页
        :param request:  view中request
        :param post_objects: 需要实现分页的对象(列表,queryset)
        :param page_number: #每页显示条数,默认20页
        :return:
        """
        #实例化结果集,    queryset转列表
        post_objects=list(post_objects)
        paginator = Paginator(post_objects, page_number) # Show 20 contacts per page
    
        #获取htmml中传递page值
        page = request.GET.get('page',1)
        try:
            #需要返回的值
            page_objs = paginator.page(page)
        except PageNotAnInteger:
            # 如果page不是整数,返回第1页
            page_objs = paginator.page(1)
        except EmptyPage:
            # 如果page超出最大值 ,只显示最后一页
            page_objs = paginator.page(paginator.num_pages)
    
        return page_objs
    View Code

      2)定义模板:paginator.html

    <!--显示记录数-->
    <div class="dataTables_info" id="editable_info" role="status" aria-live="polite">
        显示 {{ page_objs.start_index }} 至 {{ page_objs.end_index }} 条,共 {{ page_objs.paginator.count }} 条记录
    </div>
    <div class="text-right">
        {% load page_tag %}
        <ul class="pagination">
            <!--显示首页和上一页-->
            {% if page_objs.has_previous %}
                <li class="footable-page-arrow"><a data-page="first" href="?page=1">首页</a></li>
                <li class="footable-page-arrow"><a data-page="prev"
                                                   href="?page={{ page_objs.previous_page_number }}">上一页</a></li>
            {% endif %}
            <!--显示页码,只通过自定义tag,实现只显示3个页码,-->
            {% for pg in page_objs.paginator.page_range %}
                <!--参数:当前页,循环页-->
                {% page_curent_show page_objs.number pg %}
            {% endfor %}
    
            <!--显示尾页和下一页-->
            {% if page_objs.has_next %}
                <li class="footable-page-arrow"><a data-page="next" href="?page={{ page_objs.next_page_number }}">下一页</a>
                </li>
                <li class="footable-page-arrow"><a data-page="last" href="?page={{ page_objs.paginator.num_pages }}">尾页</a>
                </li>
            {% endif %}
        </ul>
    </div>
    View Code

      3)显示页码,通过自定义tag,实现只显示3个页码:page_tag.py

      

    # -*- coding:utf-8 -*-
    __author__ = 'lixiang'
    from django import template
    from django.utils.html import format_html
    register=template.Library()
    
    @register.simple_tag
    def page_curent_show(current_page,loop_page):
        """只显示3页码"""
        offset=abs(current_page-loop_page)
        if offset<3:
            if current_page==loop_page:
                #激活当前页
                page_ele='<li class="footable-page active"><a data-page="1" href="?page=%s">%s</a></li>' %(loop_page,loop_page)
            else:
                page_ele='<li class="footable-page"><a data-page="1" href="?page=%s">%s</a></li>' %(loop_page,loop_page)
    
            return format_html(page_ele)
        else:
            return ""
    View Code

      4)视图调用示例 

    from utils.pages import page
    
    def menu_list(request):
        menu_objs=models.Menu.objects.all()
        page_objs=page(request,menu_objs,2)
        return render(request, "juser/menu_list.html",{"page_objs":page_objs})
    View Code

     5)页面嵌入分页

      

                <table id="demo-foo-addrow"
                       class="table table-bordered table-hover toggle-circle footable-loaded footable default"
                       data-page-size="7">
                    <thead>
                    <tr>
                        <th data-sort-ignore="true" class="min-width footable-visible footable-first-column"></th>
                        <th data-sort-initial="true" data-toggle="true"
                            class="footable-visible footable-sortable footable-sorted">序号<span
                                class="footable-sort-indicator"></span></th>
                        <th class="footable-visible footable-sortable">菜单名<span
                                class="footable-sort-indicator"></span></th>
                        <th data-hide="phone, tablet" class="footable-visible footable-sortable">菜单位置<span
                                class="footable-sort-indicator"></span></th>
                        <th data-hide="phone, tablet" class="footable-visible footable-last-column footable-sortable">
                            操作<span
                                class="footable-sort-indicator"></span></th>
                    </tr>
                    </thead>
    
                    <tbody>
    
                    {% for menu_obj in page_objs %}
                        <tr class="footable-even" style="display: table-row;">
                            <td class="footable-visible footable-first-column">
                                <button class="demo-delete-row btn btn-danger btn-xs"><i class="demo-pli-cross"></i>
                                </button>
                            </td>
                            <td class="footable-visible"><span class="footable-toggle"></span>{{ forloop.counter }}</td>
                            <td class="footable-visible">{{ menu_obj.name }}</td>
                            <td class="footable-visible">{{ menu_obj.seq }}</td>
                            <td class="footable-visible">
                                <a><span class="label  label-success">查看</span></a>
                                <a><span class="label  label-dark">编辑</span></a>
                                <a><span class="label label-table label-danger">删除</span></a>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="7" class="footable-visible">
                            {% include "paginator.html" %}
                        </td>
                    </tr>
                    </tfoot>
                </table>
            </div>
    View Code
  • 相关阅读:
    【科创人上海行】扶墙老师王福强:架构师创业要突破思维局限,技术人创业的三种模式,健康第一
    【科创人·独家】连续创业者高春辉的这六年:高强度投入打造全球领先的IP数据库
    中国确实需要大力扩充核武器
    SAP MM 可以通过STO在公司间转移质检库存?
    SAP MM 如何看一个采购申请是由APO系统创建后同步过来的?
    SAP MM 如何看一个Inbound Delivery单据相关的IDoc?
    SAP ECC & APO集成
    SAP MM 采购订单收货之后自动形成分包商库存?
    SAP MM 带有'Return'标记的STO,不能创建内向交货单?
    SAP MM 没有启用QM的前提下可以从QI库存里退货给Vendor?
  • 原文地址:https://www.cnblogs.com/lixiang1013/p/8686818.html
Copyright © 2011-2022 走看看