分页器:配合bootstarp中的分页器使用
views.py
from django.shortcuts import render from app01.models import Book from django.core.paginator import Paginator, EmptyPage # Create your views here. def index(request): ''' 批量插入 booklist =[] for i in range(10000): book = Book(name='book%s'%i,price=i*i) booklist.append(book) Book.objects.bulk_create(booklist) #批量插入,相当于insert后面有10000条记录,而不是一万条insert :param request: :return: ''' books = Book.objects.all() pagetor = Paginator(books, 2) # Paginator(分页对象,每个分页中的数据数量) # 总页数 print('总页数:', pagetor.num_pages) # 数据总数量 print('数据总数:', pagetor.count) # 页码列表,顾头不顾尾,可以配合bootstrap的分页器使用 print('页码列表:', pagetor.page_range) # 查看某一页的数据,对应html中for book in page # page = pagetor.page(1) '''如果我们打开http://127.0.0.1:8000/index/,那么第一次的get请求是没有任何参数的.默认的request.GET.get('page'),就是空. 是无法进行看看页面数据的.一般index都是查看的第一页数据,我们给一个默认值1,即可.查看其它页面可以使用:index/?page=2这样的方式 ''' ''' 分页思路流程: 默认当前页是第一页. 如果总页数小于我们设置的show_pages,那么分页数就等于页码列表pagetor.page_range. 如果总页数大于show_pages,分3种情况 1.如果当前页面减去show_pages/2<1,那么分页列表就是(1,show_pages),不然分页会显示负数. 2.如果当前页面加上show_pages/2 大于总页数,那么分页列表就是(总页数-show_pages,总页数+1) 3.如果当前页面都不符合上面的条件(当前页面属于一个中间值,减去既不会小于0且又不大于总页数), 那么分页列表就是(当前页面-show_page/2,当前页面+show_pages/2) ''' current_page_num = int(request.GET.get('page', 1)) show_pages = 11 if pagetor.num_pages >show_pages: print("总页数大于11页") if current_page_num-5 <1: page_list = range(1,show_pages) elif current_page_num+5 >pagetor.num_pages: print("当前页数大于5") page_list =range(pagetor.num_pages-10,pagetor.num_pages+1) else: page_list =range(current_page_num-5,current_page_num+6) else: page_list =pagetor.page_range # print('当前页码:',current_page_num) # if current_page_num -5 <1: # page_range= range(1,11) # elif current_page_num+5 >pagetor.num_pages: # page_range = range(current_page_num-5,pagetor.num_pages+1) # else: # page_range =range(current_page_num-5,current_page_num+5) # else: # page_range = pagetor.page_range try: print('GET内容', request.GET) # current_page_num = int(request.GET.get('page', 1)) print('当前页:',current_page_num) # print("page_range",page_range) # 获取页面对象,用来展示页面内容的,和分页无关 page_range = pagetor.page(current_page_num) print("page_range:",page_range) ''' 以下4个分页对象的属性,可以配合bootstrap的分页器来使用,用来定义上一页或者下一页的页码 ''' # 是否有上一页 # print("是否有上一页:", current_page.has_previous()) # # 上一页页码 # print('上一页页码:', current_page.previous_page_number()) # # 是否有下一页 # print("是否有下一页:", current_page.has_next()) # # 下一页页码 # print("下一页页码:",current_page.next_page_number()) except EmptyPage as e: # print('页码不正确,即将返回首页!') error = True index_page = pagetor.page(1) return render(request, 'index.html', locals())
index.html
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>Django分页器</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> {#查某一页的数据#} {#<ul style=" 200px; margin: 0 auto">#} {#{% for book in page %}#} {# <li>{{ book.name }}:{{ book.price }}</li>#} {#{% endfor books%}#} {#</ul>#} {#{% if error %}#} {# <script>#} {# alert('页码不正确,即将返回首页!')#} {##} {# </script>#} {# <ul style=" 200px; margin: 0 auto">#} {# {% for book in index_page %}#} {# <li>{{ book.name }}:{{ book.price }}</li>#} {# {% endfor books %}#} {# </ul>#} {#{% else %}#} {# <ul style=" 200px; margin: 0 auto">#} {# {% for book in current_page %}#} {# <li>{{ book.name }}:{{ book.price }}</li>#} {# {% endfor books %}#} {# </ul>#} {##} {#{% endif %}#} <ul style=" 200px; margin: 0 auto"> {% for book in page_range %} <li>{{ book.name }}:{{ book.price }}</li> {% endfor books %} </ul> <div class="page" style="margin: 0 auto; 33%"> <nav aria-label="Page navigation"> <ul class="pagination"> {# 判断是否有上一页#} {% if page_range.has_previous %} <li><a href="?page={{ page_range.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 page_num in page_list %} {% if current_page_num == page_num %} <li class="active"><a href="?page={{ page_num }}">{{ page_num }}</a></li> {% else %} <li><a href="?page={{ page_num }}">{{ page_num }}</a></li> {% endif %} {% endfor %} {# 判断是否有下一页#} {% if page_range.has_next %} <li><a href="?page={{ page_range.next_page_number }}" aria-label="Next"> <span aria-hidden="true">下一页</span></a> </li> {% else %} <li class="disabled"><a href="" aria-label="Next"> <span aria-hidden="true">下一页</span> </a></li> {% endif %} </ul> </nav> </div> </body> </html>