Django分页

分页:

官方文档传送门

Paginator 类对象的属性:

 
  
num_pages: 返回分页之后的总页数
page_range: 返回分页后页码的列表

Paginator 类对象的方法:

 
  
page(self, number): 返回第 number 页的 Page 类实例对象

 

Page 类对象的属性:

 
  
number: 返回当前页的页码
object_list: 返回包含当前页的数据的查询集
paginator: 返回对应得 Paginator 类对象 

Page 类对象的方法:

 
  
 
        
has_previous: 判断当前页是否有前一页
has_next: 判断当前页是否有下一页
previous_page_number: 返回前一页的页码
next_page_number: 返回下一页的页码

 

实例:

 
  
from django.conf.urls import url
urlpatterns = [
    url(r'^show_area(?P<pindex>\d+))', views.show_area)
]

 

 
  
def show_area(request, pindex):
    # 1. 查询处所有省级地区的信息
    areas = AreaInfo.objects.filter(aParent_isnull=True)
    # 2. 分页, 每页显示10条数据: 即: 创建 Paginator 类的实例对象
    my_paginator = Paginator(areas, 10)
    # 打印出一共多少页
    print(my_paginator.num_pages)
    # 打印出所有页码
    print(my_paginator.page_range)
    
    # 3. 获取第 pindex 页的内容
    # page 是 Page 类的实例对象
    if pindex == '':
        pindex = 1
    else:
        pindex = int(pindex)
    page = my_paginator.page(pindex)
    # 打印当前页的页码
    print(page.number)
    
    # 4. 使用模版
    return  render(request, 'booktext/show_area.html', {'page': page})
 
  
 
        
<body>
    <ul>
        {% for area in page.object_list %}
        // 上下两者等效
        // 循环显示 当前页的所有对象
        {% for area in page %}
        <li>{{ area.atitle }}</li>
        {% endfor %}
    </ul>
    <ul>
    // 判断是否有上一页
    {% if page.has_previous %}
        <a href="/show_area{{page.previous_page_number}}">上一页</a>
    {% endif %}
    {% for pindex in page.paginator.pgae_range %}
        {% if pindex == page.number %}
        // 如果是当前页
            {{pindex}}
        {% else %}
            <a href='show_area{{ pindex }}'></a>
        {% endif %}
    {% endfor %}
     // 判断是否有下一页
    {% if page.has_next %}
        <a href="/show_area{{page.next_page_number}}">下一页</a>
    {% endif %}
    </ul>
</body>

 

官方文档

分页

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

 

示例

Paginator提供对象的列表,以及你想为每一页分配的元素数量,它就会为你提供访问每一页上对象的方法:

 
  
>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo']
>>> p = Paginator(objects, 2)
>>> p.count
4
>>> p.num_pages
2
>>> p.page_range
[1, 2]
>>> 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

注意

注意你可以向Paginator提供一个列表或元组,Django的QuerySet,或者任何带有count()__len__()方法的对象。当计算传入的对象所含对象的数量时,Paginator会首先尝试调用count(),接着如果传入的对象没有count()方法则回退调用 len()。这样会使类似于Django的QuerySet的对象使用更加高效的 count()方法,如果存在的话。

 

使用 Paginator

这里有一些复杂一点的例子,它们在视图中使用 Paginator 来为查询集分页。我们提供视图以及相关的模板来展示如何展示这些结果。这个例子假设你拥有一个已经导入的Contacts模型。

视图函数看起来像是这样:

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

list.html模板中,你会想要包含页面之间的导航,以及来自对象本身的任何有趣的信息:

 
  
<div class="pagination">
    <span class="step-links">
        
        <span class="current">
            Page  of .
        </span>
        
    </span>
</div>

 

分页器 objects

Paginator类拥有以下构造器:

  • class Paginator(object_listper_pageorphans=0allow_empty_first_page=True)[source]

     

所需参数

  • object_list

    A list, tuple, Django QuerySet, or other sliceable object with a count() or __len__() method.

    可以是一个列表, 元组, Django的结果集, 或者是任何一个可以迭代的对象

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

    在最后一页上允许的最小条目数,默认为零。当你不想要一个只有很少的项目的最后一页时,就用这个。如果最后一页通常有一些小于或等于“孤儿”的条目,那么这些条目将被添加到前一页(这是最后一页),而不是自己将条目放在页面上。例如,有23个条目,“perpage=10”和“孤儿=3”,将有两页;第一个页面有10个条目,第二个(和最后一个)页面有13个条目。

     

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

    第一个页面是否被允许为空。如果“False”和“objectlist”是空的,那么就会出现一个“错误”的错误。

 

方法

  • 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_listnumberpaginator)[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开始。

 


转载于:https://www.cnblogs.com/amou/p/9696006.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值