标签:rtc int 就会 最新 文件 lan request 总数 cross
一.分页器:
知识点先览
Paginator:Django分页器
bulk_create:批量插入数据
paginator.count:数据总数,其中paginator是分页器实例化对象
paginator.num_pages:总页数
paginator.page_range:页码的列表
object_list:分页之后的数据列表
has_next:是否有下一页
next_page_number:下一页页码
has_previous:是否有上一页
previous_page_number:上一页页码
models.py
from django.db import models # Create your models here. class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8,decimal_places=2)
views.py
# -*- encoding:utf-8 -*- from django.shortcuts import render # Create your views here. from app01.models import Book from django.core.paginator import Paginator,EmptyPage # 导入分页器 def index(request): # 方法不好,每次都要插入数据,需要插入100次 # for i in range(100): # Book.objects.create(title="book_%s"%i,price=i*i) # 批量插入,导入到数据库里 bulk_create ‘‘‘ book_list = [] for i in range(100): book_obj = Book(title="book_%s"%i,price=i*i) book_list.append(book_obj) Book.objects.bulk_create(book_list) ‘‘‘ book_list = Book.objects.all() # 拿到所有书籍数据 # 分页器 paginator = Paginator(book_list,8) # 第一个参数是需要分页的列表,第二个是每页需要展示的数目 print(paginator.count) # 数据总数 print(paginator.num_pages) # 总页数 print(paginator.page_range) # 页码的列表,总页数+1 # 100 # 13 # range(1, 14) page1 = paginator.page(1) # 第一页的page对象,固定死了 # 动态显示 try: current_page_num = int(request.GET.get("page", 1)) # get数据是通过?连接的 http://127.0.0.1:8000/index/?page=2 #做成一个动态的效果。给它个默认值拿第一页,不加1就会返回一个None。当前想要的第几页的数据 current_page = paginator.page(current_page_num) # 拿到第几页的page对象 # 显示每一页具体数据的两种方式 print("object_list",page1.object_list) # object_list < QuerySet[ < Book: Book object (1) >, < Book: Book object (2) >, < Book: Book object (3) >, # < Book: Book object (4) >, < Book: Book object (5) >, < Book: Book object (6) >, < Book: Book object (7) >, # < Book: Book object (8) >] > for i in page1: print(i) # Book object (1) # Book object (2) # Book object (3) # Book object (4) # Book object (5) # Book object (6) # Book object (7) # Book object (8) except EmptyPage as e: current_page = paginator.page(1) return render(request,"index.html",locals())
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</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> <p3>index</p3> {# 显示当前页面的书籍信息 #} <ul> {% for book in current_page %} <li>{{ book.title }}:{{ book.price }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> // 分页的页面 <ul class="pagination"> {# 无法显示上一页和下一页 #} {# <li>#} {# <a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">#} // 上一页:当前页码减一 --> href="?page={{current_page_num|add:-1}}" {# <span aria-hidden="true">上一页</span>#} {# </a>#} {# </li>#} {% if current_page.has_previous %} // 判断当前页面是否还有上一页 <li> <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"> // 获得当前页面上一页的页码 <span aria-hidden="true">上一页</span> </a> </li> {% else %} <li class="disabled"> {# bootstrap设置disabled 不能点击#} // 上一页没数据就不能点击 <a href="#" aria-label="Previous"> <span aria-hidden="true">上一页</span> </a> </li> {% endif %} {% for item in paginator.page_range %} // item为数字,这里不应该写死 --> page_range {#{% for item in page_range %}#} {% if current_page_num == item %} {# 判断当前页码是否和循环的页码一致 bootstrap设置的active会显示深色, #} <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="?page={{ item }}">{{ item }}</a></li> {% endif %} {% endfor %} {# <li>#} {# <a href="?page={{ current_page_num|add:1 }}" aria-label="Next">#} {# <span aria-hidden="true">下一页</span>#} {# </a>#} {# </li>#} {% if current_page.has_next %} // 判断当前页面是否还有下一页 <li> <a href="?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> </body> </html>
改进版:
views.py
# -*- encoding:utf-8 -*- from django.shortcuts import render # Create your views here. from app01.models import Book from django.core.paginator import Paginator,EmptyPage # 导入分页器 def index(request): # 方法不好,每次都要插入数据,需要插入100次 # for i in range(100): # Book.objects.create(title="book_%s"%i,price=i*i) # 批量插入 bulk_create ‘‘‘ book_list = [] for i in range(100): book_obj = Book(title="book_%s"%i,price=i*i) book_list.append(book_obj) Book.objects.bulk_create(book_list) ‘‘‘ book_list = Book.objects.all() # 分页器 paginator = Paginator(book_list,3) # 第一个参数是需要分页的列表,第二个是每页需要展示的数目 print(paginator.count) # 数据总数 print(paginator.num_pages) # 总页数 print(paginator.page_range) # 页码的列表,总页数+1 # 100 # 13 # range(1, 14) page1 = paginator.page(1) # 第一页的page对象,固定死了 # 动态显示 current_page_num = int(request.GET.get("page", 1)) # get数据是通过?连接的 http://127.0.0.1:8000/index/?page=2 # 固定页码数居中 if paginator.num_pages > 11: if current_page_num - 5 < 1: ## 点1,2,3,4,5,6页码不动,点7开始左五右五,点倒数五个页面不动 page_range = range(1,12) elif current_page_num + 6 > paginator.num_pages: page_range = range(paginator.num_pages-10,paginator.num_pages+1) else: page_range = range(current_page_num-5,current_page_num+6) else: page_range = paginator.page_range try: current_page = paginator.page(current_page_num) # 显示每一页具体数据的两种方式 print("object_list",page1.object_list) # object_list < QuerySet[ < Book: Book object (1) >, < Book: Book object (2) >, < Book: Book object (3) >, # < Book: Book object (4) >, < Book: Book object (5) >, < Book: Book object (6) >, < Book: Book object (7) >, # < Book: Book object (8) >] > for i in page1: print(i) # Book object (1) # Book object (2) # Book object (3) # Book object (4) # Book object (5) # Book object (6) # Book object (7) # Book object (8) except EmptyPage as e: current_page = paginator.page(1) return render(request,"index.html",locals())
index,html
{% for item in page_range %} 不应该写死,其他同上 {% if current_page_num == item %} <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="?page={{ item }}">{{ item }}</a></li> {% endif %} {% endfor %}
标签:rtc int 就会 最新 文件 lan request 总数 cross
原文地址:https://www.cnblogs.com/mumupa0824/p/10361854.html