Django-分页器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django-分页器相关的知识,希望对你有一定的参考价值。

一、分页试图函数

>>> 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-分页器的主要内容,如果未能解决你的问题,请参考以下文章

django中的分页器

Django自定义分页器

django-模型层之ajax续批量插入分页器

django-模型层之ajax续批量插入分页器

Django分页器及自定义分页器

django分页器的一些思考