Django的分页 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

以下是一个例子:
from django.core.paginator import Paginator


def log(rq):
    logs = LogInfo.objects.all().order_by('-create_time')
    page = int(rq.GET.get('page', 1))
    pe = 4
    pf = 5
    pview = pe + pf + 1
    try:
        p = Paginator(logs, 10)  //每页显示10条。
        if page <= 0:
            page = 1
        if page > p.num_pages:
            page = p.num_pages
        num_pages = p.num_pages //总页数
        part = p.page(page)  //第 page 页的内容
        pagelist = part.object_list  //第 page 页的内容的列表
        if part.has_previous():
            pre_num = part.previous_page_number()  //第 page 页的前一页
        else:
            pre_num = 0
        if part.has_next():
            next_num = part.next_page_number()  //第 page 页的后一页
        else:
            next_num = 0
        if page > pf:
            page_range = p.page_range[page - pf - 1:page + pe]
        else:
            page_range = p.page_range[0:pview]
    except Exception, e:
        print 'page ', e
        pagelist = []
        pre_num, next_num, page_range, num_pages = 0, 0, 0, 0


    return render_to_response("log.html", {'logs': pagelist, 'num_pages': num_pages,
                                           'page': page, 'page_range': page_range,
                                           'pre_num': pre_num,
                                           'next_num': next_num}


  <table id="logTable">
        <thead>
            <tr class="table-one">
                <th><input class="check-all" type="checkbox" value=""/></th>
                <th>操作内容</th>
                <th>操作时间</th>
            </tr>
        </thead>
        <tbody id="loglist">
            {% for log in logs %}
            <tr>
                <td><input name="sel_chk" type="checkbox"  value="{{ log.log_info_id }}" /></td>
                <td class="table-td-left" title="{{ log.operation_content }}">{{ log.operation_content }}</td>
                <td title="{{ log.create_time|date:'Y-m-d H:i:s' }}">{{ log.create_time|date:"Y-m-d H:i:s" }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>


    <a href="/logs/log_detail/?page=1">首页</a>
    {% if pre_num > 0 %}
        <a href="/logs/log_detail/?page={{ pre_num }}>上一页</a>
    {% endif %}
    {% for pl in page_range %}
        {% if pl %}
            {%if pl == page %}
                {{ pl }}
            {% else %}
                <a href="/logs/log_detail/?page={{ pl }}">{{ pl }}</a>
            {% endif %}
        {% endif %}
    {% endfor %}
    {% if page < num_pages %}
        <a href="/logs/log_detail/?page={{ next_num }}">下一页</a>
    {% endif %}
    <a href="/logs/log_detail/?page={{ num_pages }}">尾页</a>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值