美文网首页
python-Django 分页功能的实现

python-Django 分页功能的实现

作者: yunpiao | 来源:发表于2018-03-10 22:05 被阅读0次

Django提供了一些类来帮助你管理分页的数据 -- 也就是说,数据被分在不同页面中,并带有“上一页/下一页”标签。这些类位于django/core/paginator.py中。

python 代码实现 后台逻辑


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

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

    return render_to_response('list.html', {"contacts": contacts})
    

Django前端显示部分

<html>
 <head></head>
 <body>
  <table width="100%" class="table table-striped table-bordered table-hover"> 
   <thead> 
    <tr> 
     <th> uuid</th> 
     <th> 任务名称</th> 
     <th> 任务结果</th> 
     <th> 任务状态</th> 
     <th> 接收时间</th> 
     <th> 完成时间</th> 
     <th> 传入参数</th> 
    </tr> 
   </thead> 
   <tbody>
     {% for i, v in model %} 
    <tr> 
     <td> 
      <form method="POST" action="{% url " celery_api_task"="" v.uuid="" %}"="">
        {% csrf_token %} 
       <button type="submit" class="btn btn-default btn-xs" title="任务详情"> {{ v.uuid }} </button> 
      </form> </td> 
     <td> {{ v.name }}</td> 
     <td> {{ v.result }}</td> 
     <td> {{ v.state }}</td> 
     <td> {{ v.received }}</td> 
     <td> {{ v.timestamp }}</td> 
     <td> {{ v.args }}</td> 
     <td> </td> 
    </tr> {% endfor %} 
   </tbody> 
  </table> 
  <div class="col-md-offset-3 " style="">
    {% bootstrap_paginate model range=10 show_prev_next=&quot;true&quot; show_first_last=&quot;true&quot; align=&quot;center&quot; %} 
  </div> 
 </body>
</html>

引用

分页器 objects

Paginator类拥有以下构造器:

class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)[source]¶

所需参数¶

> object_list
    A list, tuple, Django QuerySet, or other sliceable object with a count() or __len__() method.
> per_page
    The maximum number of items to include on a page, not including orphans (see the orphans optional argument below).

可选参数¶

> orphans
    The minimum number of items allowed on the last page, defaults to zero. Use this when you don’t want to have a last page with very few items. If the last page would normally have a number of items less than or equal to orphans, then those items will be added to the previous page (which becomes the last page) instead of leaving the items on a page by themselves. For example, with 23 items, per_page=10, and orphans=3, there will be two pages; the first page with 10 items and the second (and last) page with 13 items.
    
> allow_empty_first_page
Whether or not the first page is allowed to be empty. If False and object_list is empty, then an EmptyPage error will be raised.

方法¶

> Paginator.page(number)[source]¶
返回在提供的下标处的Page对象,下标以1开始。如果提供的页码不存在,抛出InvalidPage异常。

属性¶

> Paginator.count¶
    所有页面的对象总数。

注意


当计算object_list所含对象的数量时, Paginator会首先尝试调用object_list.count()。如果object_list没有 count() 方法,Paginator 接着会回退使用len(object_list)。这样会使类似于Django’s QuerySet的对象使用更加便捷的count()方法,如果存在的话。

Paginator.num_pages¶
页面总数。

Paginator.page_range¶
页码的范围,从1开始,例如[1, 2, 3, 4]。

InvalidPage exceptions

exception InvalidPage[source]¶
异常的基类,当paginator传入一个无效的页码时抛出。

Paginator.page()放回在所请求的页面无效(比如不是一个整数)时,或者不包含任何对象时抛出异常。通常,捕获InvalidPage异常就够了,但是如果你想更加精细一些,可以捕获以下两个异常之一:

exception PageNotAnInteger[source]¶
当向page()提供一个不是整数的值时抛出。

exception EmptyPage[source]¶
当向page()提供一个有效值,但是那个页面上没有任何对象时抛出。

这两个异常都是InvalidPage的子类,所以你可以通过简单的except InvalidPage来处理它们。

Page objects

你通常不需要手动构建 Page对象 -- 你可以从Paginator.page()来获得它们。

class Page(object_list, number, paginator)[source]¶
当调用len()或者直接迭代一个页面的时候,它的行为类似于 Page.object_list 的序列。

方法¶

Page.has_next()[source]¶
如果有下一页,则返回True。

Page.has_previous()[source]¶
如果有上一页,返回 True。

Page.has_other_pages()[source]¶
如果有上一页或下一页,返回True。

Page.next_page_number()[source]¶
返回下一页的页码。如果下一页不存在,抛出InvalidPage异常。

Page.previous_page_number()[source]¶
返回上一页的页码。如果上一页不存在,抛出InvalidPage异常。

Page.start_index()[source]¶
返回当前页上的第一个对象,相对于分页列表的所有对象的序号,从1开始。比如,将五个对象的列表分为每页两个对象,第二页的start_index()会返回3。

Page.end_index()[source]¶
返回当前页上的最后一个对象,相对于分页列表的所有对象的序号,从1开始。 比如,将五个对象的列表分为每页两个对象,第二页的end_index() 会返回 4。

属性¶

Page.object_list¶
当前页上所有对象的列表。

Page.number¶
当前页的序号,从1开始。

Page.paginator¶
相关的Paginator对象。

相关文章

  • python-Django 分页功能的实现

    Django提供了一些类来帮助你管理分页的数据 -- 也就是说,数据被分在不同页面中,并带有“上一页/下一页”标签...

  • (14)Django - 分页功能

    Django已为开发者内置了分页功能,只需调用Django内置分页功能的函数即可实现数据分页功能。我们在Djang...

  • 十二、MyBatis实现分页功能

    一、本课目标 掌握MyBatis分页实现 二、MyBatis分页功能实现 需求说明: 为用户管理之查询用户列表功能...

  • flask实现分页

    原文地址数据库实现分页offset:使用offset可以实现数据库分页功能questions = Question...

  • MyBatis分页和动态标签

    MyBatis分页 内存分页 MyBatis提供了RowBounds类实现内存分页功能。其原理是首先根据sql语句...

  • 分页功能的实现

    直接上代码吧: 基础版 高级版(增加了url参数的保留功能) 分页的使用: ps:在python脚本中调用djan...

  • Springboot+Mybatis+mysql实现分页查询

    分页功能是一个很常见的功能点,今天用PageHelper实现了简单的分页查询功能,记录下使用过程,供而大家参考. ...

  • Pagehelper分页插件的使用

    概述: Pagehelper:是中国的开源的mybatis分页插件,通过该插件可以非常简单的实现分页功能; ...

  • 如何优雅地实现分页查询

    分页功能是很常见的功能,特别是当数据量越来越大的时候,分页查询是必不可少的。实现分页功能有很多种方式,如果使用的O...

  • AngularJs 指令和过滤器实现分页

    这里实际上实现的不是分页,不过跟分页功能比较类似了。这里实现的功能是 限制每一列显示商品的数量,这一列显示不下的 ...

网友评论

      本文标题:python-Django 分页功能的实现

      本文链接:https://www.haomeiwen.com/subject/phorfftx.html