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

【前端】Django分页器

时间:2017-08-30 20:49:56      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:html   false   cts   most   bootstra   short   oct   contain   index   

一、分页器(paginator)

分页器的使用

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

实现一个分页效果:

Template:

{% 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>

views:

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分页器

标签:html   false   cts   most   bootstra   short   oct   contain   index   

原文地址:http://www.cnblogs.com/78pikaqiu/p/7455046.html

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