诚意
诚意如你,当一诚的态度对待

导航

 

from django.core.paginator import Paginator

一:分页器的基本使用方法

from django.core.paginator import Paginator
def index(request):
    book_list = Book.objects.all()

    '''
    批量导入数据:

    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()  #book表记录(一行一行数据)的对象

    paginator = Paginator(book_list, 10)  #每页显示10条#Paginator对象
    print("count:",paginator.count)           #数据总数(总共多少行数据)
    print("num_pages",paginator.num_pages)    #总页数(总共多少页)
    print("page_range",paginator.page_range)  #页码的列表

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

    print(page.has_next())  # 下一页
    print(page.has_previous())  # 上一页   True  或False
    print(page.next_page_number())  #下一页的页码号
    print(page.previous_page_number())#上一页的页码号
    '''

    return render(request, 'index.html', {'book_list': book_list})

 二:分页器的使用案例

在项目的基础上:项目--图书管理系统--第三阶段--ajax局部刷新

1:前端浏览器

 1 {% extends 'base.html' %}
 2 
 3 {% block content %}
 4     <div class="col-md-10">
 5         <a class="btn  btn-success" href="{% url 'add' %}" role="button" style="margin-top: 20px">添加书籍</a>
 6         <table class="table table-striped table-hover table-bordered">
 7             <thead>
 8             <tr>
 9                 <th>编号</th>
10                 <th>书籍名称</th>
11                 <th>价格</th>
12                 <th>出版日期</th>
13                 <th>出版社</th>
14                 <th>作者</th>
15                 <th>操作</th>
16             </tr>
17 
18             </thead>
19             <tbody>
20 
21             {#            循环动态显示信息里面的数据#}
22             {% for book in current_page %}
23                 <tr>
24                     {#                    显示编号#}
25                     <th>{{ forloop.counter }}</th>
26                     <th>{{ book.title }}</th>
27                     <th>{{ book.price }}</th>
28                     <th>{{ book.pub_date|date:'Y-m-d' }}</th>
29                     <th>{{ book.publish }}</th>
30                     <th>
31 {#                        跨表获取数据#}
32                             {% for author in book.authors.all %}
33                                 {{ author.name }}
34 {#                                显示多个数据之间用的逗号forloop.last    如果这是最后一次循环,则为真#}
35                                 {% if not forloop.last %}
36                                 ,
37                                 {% endif %}
38                             {% endfor %}
39 
40                     </th>
41                      <th>
42                             <a class="btn btn-danger btn-sm" href="{% url 'edit' book.pk %}" role="button">编辑</a>
43                             <a class="btn btn-warning btn-sm" href="{% url 'delete' book.pk %}" role="button">删除</a>
44                      </th>
45                 </tr>
46             {% endfor %}
47             </tbody>
48         </table>
49 
50     </div>
51 {% endblock %}
index.html

 

注意:接收后端模板语法变量变了

2:后端Django-ORM服务器

urls.py

path('index/', views.index,name='index')

 

views.py

from django.core.paginator import Paginator
def index(request):
    book_list = Book.objects.all()

    paginator = Paginator(book_list, 5)
    current_page_num=request.GET.get('page',1) #如果取不到数据,就取第一页的#取值page是多少
    current_page=paginator.page(current_page_num)#显示第几页数据
    return render(request, 'index.html', {'current_page': current_page})

 

效果:

url不加page,默认第一页

 

 上面这种对于客户来说太不友好,想看第几页还得在地址栏输入,那么有没有其他方法呢?

 

三:分页器案例优化

1:前端-页面

 1 {% extends 'base.html' %}
 2 
 3 {% block content %}
 4     <div class="col-md-10">
 5         <a class="btn  btn-success" href="{% url 'add' %}" role="button" style="margin-top: 20px">添加书籍</a>
 6         <table class="table table-striped table-hover table-bordered">
 7             <thead>
 8             <tr>
 9                 <th>编号</th>
10                 <th>书籍名称</th>
11                 <th>价格</th>
12                 <th>出版日期</th>
13                 <th>出版社</th>
14                 <th>作者</th>
15                 <th>操作</th>
16             </tr>
17 
18             </thead>
19             <tbody>
20 
21             {#            循环动态显示信息里面的数据#}
22             {% for book in current_page %}
23                 <tr>
24                     {#                    显示编号#}
25                     <th>{{ forloop.counter }}</th>
26                     <th>{{ book.title }}</th>
27                     <th>{{ book.price }}</th>
28                     <th>{{ book.pub_date|date:'Y-m-d' }}</th>
29                     <th>{{ book.publish }}</th>
30                     <th>
31                         {#                        跨表获取数据#}
32                         {% for author in book.authors.all %}
33                             {{ author.name }}
34                             {#                                显示多个数据之间用的逗号forloop.last    如果这是最后一次循环,则为真#}
35                             {% if not forloop.last %}
36                                 ,
37                             {% endif %}
38                         {% endfor %}
39 
40                     </th>
41                     <th>
42                         <a class="btn btn-danger btn-sm" href="{% url 'edit' book.pk %}" role="button">编辑</a>
43                         <a class="btn btn-warning btn-sm" href="{% url 'delete' book.pk %}" role="button">删除</a>
44                     </th>
45                 </tr>
46             {% endfor %}
47             </tbody>
48         </table>
49 
50     </div>
51     <nav aria-label="Page navigation">
52         <ul class="pagination">
53 
54             {% if current_page.has_previous %}
55                 <li>
56                     <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous">
57                         <span aria-hidden="true">上一页</span>
58                     </a>
59                 </li>
60             {% else %}
61                 <li class="disabled"><a href="">上一页</a></li>
62             {% endif %}
63 
64             {#            页码的显示#}
65             {% for num in paginator.page_range %}
66                 {#                数字    字符串  不能比较#}
67                 {% if num == current_page_num %}
68                     <li class="active"><a href="?page={{ num }}">{{ num }}</a></li>
69                 {% else %}
70                     <li><a href="?page={{ num }}">{{ num }}</a></li>
71                 {% endif %}
72 
73             {% endfor %}
74 
75             {% if current_page.has_next %}
76                 <li>
77                     <a href="?page={{ current_page.next_page_number }}" aria-label="next">
78                         <span aria-hidden="true">下一页</span>
79                     </a>
80                 </li>
81             {% else %}
82                 <li class="disabled"><a href="">下一页</a></li>
83             {% endif %}
84         </ul>
85     </nav>
86 {% endblock %}
index.html

 

2:后端-view.py

from django.core.paginator import Paginator,EmptyPage   #EmptyPage:一个错误类型
def index(request):

    book_list = Book.objects.all()
    paginator = Paginator(book_list, 5)
    try:   #如果取超过页码的页面会报错
        current_page_num=request.GET.get('page',1) #如果取不到数据,就取第一页的#取值page是多少
        current_page=paginator.page(current_page_num)#显示第几页数据
    except EmptyPage as e:
        current_page_num=1
        current_page = paginator.page(1)

    return render(request, 'index.html', {'current_page': current_page,'paginator':paginator,'current_page_num':int(current_page_num)})

 

效果:

 

 缺点:如果页数太多,那就会显示太多,下一步优化显示下面的效果

 

posted on 2018-10-30 23:52  诚意  阅读(170)  评论(0编辑  收藏  举报