view部分:

from django.shortcuts import render,HttpResponse

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

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)

    print("count:",paginator.count)           #数据总数
    print("num_pages",paginator.num_pages)    #总页数
    print("page_range",paginator.page_range)  #页码的列表



    page1=paginator.page(1) #第1页的page对象
    for i in page1:         #遍历第1页的所有数据对象
        print(i)

    print(page1.object_list) #第1页的所有数据


    page2=paginator.page(2)

    print(page2.has_next())            #是否有下一页
    print(page2.next_page_number())    #下一页的页码
    print(page2.has_previous())        #是否有上一页
    print(page2.previous_page_number()) #上一页的页码



    # 抛错
    #page=paginator.page(12)   # error:EmptyPage

    #page=paginator.page("z")   # error:PageNotAnInteger

    '''


    book_list=Book.objects.all()  #获取数据库中的所有数据

    paginator = Paginator(book_list, 10) #设置一页显示10条数据
    page = request.GET.get('page',1)   #获取当前页数
    currentPage=int(page)   #转换为数字类型,方便使用
  strat_num = (current_page_num - 1) * 10  #如果页面数据有序号的话,序号也要分页显示,一起传给前端,然后{{ forloop.counter|add:strat }}就可以了

    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",{"book_list":book_list,"paginator":paginator,"currentPage":currentPage})

 

前端:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>查看书籍</title>
    <link href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>

    <style>
        .container {
            margin-top: 50px;
        }
    </style>

</head>
<body>
<h3>查看书籍</h3>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <table class="table table-hover table-striped">
                <a href="{% url 'addbook' %}" class="btn btn-warning">添加书籍</a>
                <thead>
                <tr>
                    <th>编号</th>
                    <th>书籍名称</th>
                    <th>价格</th>
                    <th>出版日期</th>
                    <th>出版社</th>
                    <th>作者</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for book in current_page %}
                    <tr>
                        <td>{{ forloop.counter|add:strat }}</td>
                        <td>{{ book.title }}</td>
                        <td>{{ book.price }}</td>
                        <td>{{ book.pub_date|date:'Y-m-d' }}</td>
                        <td>{{ book.publish.name }}</td>
                        <td>
                            {% for author in book.authors.all %}
                                {{ author.name }}
                                {% if not forloop.last %}
                                    ,
                                {% endif %}
                            {% endfor %}

                        </td>
                        <td>
                            <a class="btn btn-success btn-xs" href="/books/edit/{{ book.pk }}">编辑</a>
                            <button class="btn btn-danger btn-xs del" pk="{{ book.pk }}">删除</button>
                        </td>
                    </tr>

                {% endfor %}

                </tbody>
            </table>

//以下是分页代码
            <nav aria-label="Page navigation">
                <ul class="pagination pull-right">
                    {% if current_page.has_previous %}
                        <li><a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>
                    {% else %}
                        <li class="disabled"><a href="">上一页</a></li>
                    {% endif %}
                
                    {% for num in paginator.page_range %}
                        {% if num == current_page_num %}
                            <li class="active"><a href="?page={{ num }}">{{ num }}</a></li>
                        {% else %}
                            <li><a href="?page={{ num }}">{{ num }}</a></li>
                        {% endif %}
                    {% endfor %}

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

</script>
</body>
</html>