码迷,mamicode.com
首页 > 其他好文 > 详细

Django-分页器

时间:2017-08-31 19:14:59      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:div   other   ring   ...   creat   files   cti   nat   异常   

一、分页试图函数

>>> 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)  # `<type ‘rangeiterator‘>` in Python 2.
<class range_iterator>
>>> p.page_range        #页码的列表
range(1, 3)     # =========[1,2]
 
>>> page1 = p.page(1)   #第1页的page对象
>>> page1
<Page 1 of 2>
>>> page1.object_list   #第1页的数据
[john, paul]
 
>>> page2 = p.page(2)
>>> page2.object_list    #第2页的数据
[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() # 本页第一条记录的序数(从1开始)
3
>>> page2.end_index() # 本页最后录一条记录的序数(从1开始)
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

二、分页实例

  前端展示页面:

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static ‘bootstrap.css‘ %}">
</head>
<body>
<div class="container">
    <h4>分页器</h4>
    <ul>
    {% for book in book_list %}
         <li>{{ book.title }} {{ book.price }}</li>
    {% endfor %}
</ul>
    <ul class="pagination" id="pager">
                 {% if book_list.has_previous %} <!--判断是否有前页-->
                    <li class="previous"><a href="/blog/?page={{ book_list.previous_page_number }}">上一页</a></li>
                 {% else %}
                    <li class="previous disabled"><a href="#">上一页</a></li>  <!--没有前页就禁用-->
                 {% endif %}
                 {% for num in paginator.page_range %}
                     {% if num == currentPage %}
                       <li class="item active"><a href="/blog/?page={{ num }}">{{ num }}</a></li>
                     {% else %}
                       <li class="item"><a href="/blog/?page={{ num }}">{{ num }}</a></li>
                     {% endif %}
                 {% endfor %}
                 {% if book_list.has_next %}
                    <li class="next"><a href="/blog/?page={{ book_list.next_page_number }}">下一页</a></li>
                 {% else %}
                    <li class="next disabled"><a href="#">下一页</a></li>
                 {% endif %}
            </ul>
</div>
</body>
</html>

  后台处理逻辑:

  

from django.shortcuts import render,HttpResponse

# Create your views here.
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from app01.models import *
def index(request):
    ‘‘‘
    批量导入数据:
    Booklist=[]
    for i in range(100):
        Booklist.append(Book(title="book"+str(i),price=30+i*i))
    Book.objects.bulk_create(Booklist)
    ‘‘‘

    book_list=Book.objects.all()
    paginator = Paginator(book_list, 10)
    page = request.GET.get(page,1)
    currentPage=int(page)

    try:
        print(page)
        book_list = paginator.page(page)
    except PageNotAnInteger:
        book_list = paginator.page(1)
    except EmptyPage:
        book_list = paginator.page(paginator.num_pages)

    return render(request,"index.html",locals())

 

  

Django-分页器

标签:div   other   ring   ...   creat   files   cti   nat   异常   

原文地址:http://www.cnblogs.com/vipchenwei/p/7456414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!