标签:obj 分页器 最大 css create font 显示 paginator size
from django.core.paginator import Paginator
from django.core.paginator import Paginator 先拿到分页器对象 paginator = Paginator(book_list,3) 第一个参数:对象列表,第二个参数:每页显示的条数 paginator.count 总条数 paginator.num_pages 总页数 paginator.page_range 页码数列表 current_page = paginator.page(5) 取某一页,返回一个对象 current_page.object_list 某一页里所有数据,例如:这是第5也所有数据 current_page.has_next() 是否有下一页 current_page.has_previous() 是否有上一页 current_page.next_page_number() 下一页的页码数 current_page,previous_page_number() 上一页的页码数
分页器逻辑分析
(1)批量插入 bulk_create li = [] for i in range(100): li.append(models.Book(name=‘图书%s‘%i,price=10+i)) models.Book.objects.bulk_create(li) (2)分页器分析: 先前台传过来哪一页,后台取哪一页 current_page = paginator.page(5) 前台可以循环当前页的对象 paginator.page 也可以循环当前页的内容 current_page.object_list 可以直接渲染出哪一页的页面内容 页码数分析:最大页数小于<11,就显示最大页数 最大页数大于11时,再判断我取的页数 如果我取的页数 小于 5 则 paginator.page_range 页码数列表 为 paginator.page_range(1,12) 如果我取的页数 大于 最大页数 则 paginator.page_range(paginator.num_pages-11,paginator.num_pages+1) 其他 则 get 发过来的页码数 +5 和 -5 current_range = range(current_page_num-5, current_page_num-5)
from django.core.paginator import Paginator def index(request): book_list = models.Book.objects.all() paginator = Paginator(book_list,3) 对所有数据进行分页 # page_range = paginator.page_range # 页码数列表 # print(page_range) try: # 捕捉前台传过来的数据,传过来不正常的数据都跳到第一页 current_page_num = int(request.GET.get(‘page‘)) # 前台传过来的要拿一页 current_page = paginator.page(current_page_num) # 拿哪一页 print(current_page.object_list) # 拿哪一页的所有数据 # # 这可以循环当前页的对象 paginator.page 也可以循环当前页的内容 current_page.object_list # # for item in current_page: # # print(item.name) # if paginator.num_pages > 11: # 判断总页数是否大于 11 页 if current_page_num -5 < 1: current_range = range(1,12) elif current_page_num + 5 > paginator.num_pages: current_range = range(paginator.num_pages-10, paginator.num_pages+1) else: current_range = range(current_page_num-5, current_page_num-5) else:page_range = paginator.page_range # except Exception as e: current_page_num = 1 current_page = paginator.page(current_page_num) current_range = range(1, 12) return render(request, ‘index.html‘, locals())
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css"> <script src="/static/jquery-3.3.1.js"></script> <title>图书列表</title> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <table class="table table-hover"> <thead> <tr> <th>书名</th> <th>价格</th> </tr> </thead> <tbody> {% for book in current_page %} <tr> <td>{{ book.name }}</td> <td>价格{{ book.price }}</td> </tr> {% endfor %} </tbody> </table> <div> <nav aria-label="Page navigation"> <ul class="pagination"> {% if current_page.has_previous %} <li> <a href="/index/?page={{ current_page.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 item in page_range %} {% if current_page_num == item %} <li class="active"><a href="/index/?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="/index/?page={{ item }}">{{ item }}</a></li> {% endif %} {% endfor %} {% if current_page.has_next %} <li> <a href="/index/?page={{ current_page.next_page_number }}" aria-label="Next"> <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> </div> </div> </body> </html>
标签:obj 分页器 最大 css create font 显示 paginator size
原文地址:https://www.cnblogs.com/liu--huan/p/9992459.html