We do Django project, large amount of data, we need pager (paginator) to do the page display, the effect is as follows
Method is as follows:
In views.py view function, the introduction Paginator
1 from django.core.paginator import Paginator
First, we said here about the main methods of pager:
. 1 . 1 . Entire table 2 Total paginator.count data . 3 paginator.num_pages Pages 4 list paginator.page_range page number . 5 2 . This page . 6 curuent_page.has_next () if a next page . 7 curuent_page.next_page_number () next page page 8 curuent_page.has_previous () is there a Previous 9 curuent_page.previous_page_number () on the previous page
The following is a specific code implementation:
1.views.py
. 1 from django.shortcuts Import the render, the HttpResponse, the redirect 2 . 3 from app01.models Import * # application database table . 4 from django.core.paginator Import Paginator, EmptyPage, PageNotAnInteger # introduced into the sorter . 5 . 6 DEF Demo (requerst): . 7 . 8 = Book.objects.all book_list () # get all data . 9 10 the paginator = Paginator (book_list,. 3) # are paged data and how much data as a row . 11 pag_num = paginator.num_pages # obtain the total of the entire table pages 12 is curuent_page_num = int (requerst.GET.get ( ' Page ' ,. 1)) # Get the current page, by default. 1 13 is curuent_page = paginator.page (curuent_page_num) # obtain data of this page 14 15 16 IF pag_num <. 11: # determines whether this page is less than 11 . 17 pag_range = paginator.page_range 18 is elif pag_num>. 11 : . 19 iF curuent_page_num <. 6 : 20 is pag_range Range = (1,11 ) 21 is elif curuent_page_num> (paginator.num_pages) -5 : 22 is pag_range = range(pag_num-9, pag_num+1) 23 else: 24 pag_range = range(curuent_page_num-5,curuent_page_num+5) #当前页+5大于最大页数时 25 26 27 28 29 return render(requerst,"demo.html",{'book_list' :book_list,"pagintor" : paginator,"current_Page":curuent_page,"current_Page_num":curuent_page_num,"pag_range":pag_range})
2.demo.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>分页器</title> 6 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"7# Introduction of Bootstrap > 8 </head> 9 <body style="margin-top: 100px"> 10 <div class="container"> 11 <div class="row"> 12 <div class="col-xs-6 col-xs-offset-3 col-md-6 col-md-offset-3"> 13 <div class="panel panel-default"> 14 <!-- Table --> 15 <table class="table table-striped table-bordered table-hover"> 16 < Thead > . 17 < TR > 18 is < TH > Books ID </ TH > . 19 < TH > Books Title </ TH > 20 is < TH > Books Price </ TH > 21 is </ TR > 22 is </ thead > 23 is < tbody > 24 {%}% for Book in current_page 25 < TR > 26 is < TD >{{ book.pk }}</td> 27 <td>{{ book.title }}</td> 28 <td>{{ book.price }}</td> 29 </tr> 30 {% endfor %} 31 32 </tbody> 33 </table> 34 </div> 35 36 37 <div> 38 <nav aria-label="Page navigation"> 39 <ul class="pagination"> 40 {% if not current_Page.has_previous %}<!--判断是否有上一页--> 41 <li class="disable"> 42 <a href="#" aria-label="Previous"> 43 <span aria-hidden="true">上一页</span> 44 </a> 45 </li> 46 {% else %} 47 <li> 48 <a href="?page={{ current_Page.previous_page_number }}" aria-label="Previous"> 49 <span aria-hidden="true">上一页</span> 50 </a> 51 </li> 52 {% endif %} 53 54 {% for page_range in pag_range %} 55 {% if current_Page_num == page_range %}<! - determining whether the number of pages to traverse this page, the background color is blue is added .avtive -> 56 is < Li class = "Active" > < A the href = "Page page_range = {{}}?" > {{page_range}} </ A > </ Li > 57 is {% the else%} 58 < Li > < A the href = "? Page = {{page_range}}" > {{page_range}} </ A > </ Li > 59 {% endif %} 60 {% endfor %} 61 62 {% if not current_Page.has_next %}<!-- 判断是否最后一页 --> 63 <li class="disable"> 64 <a href="?page={{ current_Page_num }}" aria-label="Next"> 65 <span aria-hidden="true">下一页</span> 66 </a> 67 </li> 68 {% else %} 69 <li> 70 <a href="?page={{ current_Page.next_page_number }}" aria-label="Next"> 71 <span aria-hidden="true">下一页</span> 72 </a> 73 </li> 74 {% endif %} 75 76 </ul> 77 </nav> 78 </div> 79 </div> 80 </div> 81 </div> 82 </body> 83 </html>
Finally, the routing switch setting is correct, you can
E.g:
1 path('demo/',views.demo)
Pager so complete! ! !