from django.core.paginator import Paginatorhtml
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 {% 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 %}
注意:接收后端模板语法变量变了ajax
urls.pydjango
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 {% 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 %}
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)})
缺点:若是页数太多,那就会显示太多,下一步优化显示下面的效果app