Python's Archiver

為方便港臺同胞閱覽,Python中國特別推出簡繁體內容轉換功能

zjxplq 发表于 2008-11-18 14:52

Django分页处理

Django提供了一系列类,帮助你管理分页数据。这些类在django/core/paginator.py

[size=4][b]例子[/b][/size][code]
>>> 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()
3
>>> 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)
...
EmptyPage: That page number is less than 1
>>> p.page(3)
...
EmptyPage: That page contains no results
[/code][size=3][b]注[/b][/size]
  能提供给Paginator的对象可以是列表与元组,元素可以是Django QuerySet或是定义过count()或__len__()方法的对象。当需确定包含元素数量时,Paginator首先试着调用count(),不然调用len()。

[size=4][b]在视图中使用Paginator[/b][/size]
  在一个视图中使用Paginator分页queryset的例子相当复杂。给与视图与相应的模板来演示显示显示结果。这个例子假设你有一个Contacts model。
  视图函数如下:[code]
from django.core.paginator import Paginator, InvalidPage, EmptyPage

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

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        contacts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('list.html', {"contacts": contacts})
[/code]在模板list.html中,需要包含页面之间的分面信息:[code]
{% for contact in contacts.object_list %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br />
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
[/code][size=4][b]Paginator对象[/b][/size]
  类Paginator:
  class Paginator(object_list,per_page,orphans=0,allow_empty_first_page=True)
  必须提供的参数:
  object_list:一个列表或元组,元素是django QuerySet或是包含count()或__len__()方法的可切片对象。
  per_page:包含在一页中最多的条目数量。
  可选参数:
  orphans:在最后一页中充许的最少条目数量,默认是0.当最后一页条目数量小于或等于orphans时,这些条目加到本页的上一页中。
  allow_empty_first_page:是否充许第一页为空。如设为False且object_list为空,则抛出EmptyPage异常。

[size=4][b]方法[/b][/size]
  Paginator.page(number):返回一个Page对象,序号是始于1.如给出的页号不存在,抛出InvalidPage异常。

[size=4][b]属性[/b][/size]
  Paginator.num_pages:页面总页数
  Paginator.page_range:页面数的范围,始于1,如[1,2,3,4]。

[size=4][b]InvalidPage异常[/b][/size]
  如要求的页面无效或页面中没有对象,page()抛出InvalidPage异常。

  PageNotAnInterger:当提供给page()的数不是整数是抛出该异常。
  EmptyPage:当提供给page()的数是一个有效数,但在该页没有对象存在时,抛出该异常。


[size=4][b]Page对象[/b][/size]

  class Page(object_list,number,paginator):
  一般不手工创建Pages,可以使用Paginator.page().

  方法:
  Page.has_next():如有下一页则返回True
  Page.has_previous():如有上一页则返回True
  Page.has_other_pages():如有上一页或下一页返回True
  Page.next_page_number():返回下一页的页码。不管下一页是否存在都返回。
  Page.previous_page_number():返回上一页的页码。不管上一页是否存在都返回。
  Page.start_index():返回当前页面中第一个对象的序号,序号始于1.例如:将一个包含5个对象的列表分成每页2个对象,则第二页的start_index()返回3.
  Page.end_index():返回当前页面中最一个对象的序号。

  属性
  Page.object_list:当前页面中所有的对象
  Page.number:当前页面的页码,始于1
  Page.paginator:页面相关的Pageinator对象。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.