django分页器

1.实例化产生一个分页器对象

# 两个参数:object_list:对象列表, per_page:每页显示的条数
paginator=Paginator(book_list,10)

2.对象内的属性

#数据总条数
paginator.count
# 总页数
paginator.num_pages
# 页码数的列表
paginator.page_range
# 取到第 x 页 ,返回一个Page对象
current_page=paginator.page(5)
# 当前页码内所有的数据
current_page.object_list
# 是否有下一页
current_page.has_next()
# 是否有上一页
print(current_page.has_previous())
# 下一页页码数
current_page.next_page_number()
# 上一页的页码数
print(current_page.previous_page_number())

3.一个简单的含有分页器的页面

前端页面

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    {% load static %}
    <script src="{% static 'jquery-3.3.1.js' %}"></script>
    <script src="{% get_static_prefix %}bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <title>分页图书</title>

</head>
<body>
<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <table class="table table-hover">
            <thead>
            <tr>
                <th>id</th>
                <th>书名</th>
                <th>价格</th>
            </tr>
            </thead>
            <tbody>
            {% for book in page_content %}
                <tr>
                    <td>{{ book.id }}</td>
                    <td>{{ book.name }}</td>
                    <td>{{ book.price }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <li>
                    <a href="?page=1" aria-label="Previous">
                        <span aria-hidden="true">首页</span>
                    </a>
                </li>
                {% if page_content.has_previous %}
                    <li>
                        <a href="?page={{ page_content.previous_page_number }}" aria-label="Previous">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="" aria-label="Previous">
                            <span aria-hidden="true">上一页</span>
                        </a>
                    </li>
                {% endif %}

                {% for page in page_count %}
                    {% if page_num == page %}
                        <li class="active"><a href="?page={{ page }}">{{ page }}</a></li>
                    {% else %}
                        <li><a href="?page={{ page }}">{{ page }}</a></li>
                    {% endif %}

                {% endfor %}


                {% if page_content.has_next %}
                    <li>
                        <a href="?page={{ page_content.next_page_number }}" aria-label="Previous">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                {% else %}
                    <li class="disabled">
                        <a href="" aria-label="Previous">
                            <span aria-hidden="true">下一页</span>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
    </div>
</div>
</body>
</html>

后台页面

from django.shortcuts import render
from app01 import models
from django.core.paginator import Paginator


# Create your views here.

def book(request):
    # book_li = []
    # for i in range(100):
    #     book_li.append(models.Book(name='book%s'%i,price=10+i))
    # models.Book.objects.bulk_create(book_li)
    if request.method == 'GET':
        books = models.Book.objects.all()
        paginator = Paginator(books, 5)
        try:
            if int(request.GET.get('page')) not in range(1, paginator.num_pages + 1):
                page_num = 1
        except Exception as e:
            page_num = 1
        else:
            page_num = int(request.GET.get('page'))
        page_content = paginator.page(page_num)
        if  paginator.num_pages<11:
            page_count = paginator.page_range
        else:
            if page_num-5<1:
                page_count = range(1,12)
            elif page_num+6>paginator.num_pages:
                page_count = range(paginator.num_pages-10,paginator.num_pages+1)
            else:
                page_count = range(page_num-5,page_num+6)

    return render(request, 'book.html', locals())
posted @ 2018-11-20 20:40  周建豪  阅读(116)  评论(0编辑  收藏  举报