2020年10月20日星期二

Django——完美的分页器

一、分页器组件介绍

1 项目数据量大了以后,比如涉及到分页,一页一页的加载显示2 django中分页器组件,把分页常用的东西,封装到一个类中3 实例化得到一个对象,对象里有属性和方法

二、模拟产生需要的数据

这里自己造出来数据

 (1)造出表  models.py   

from django.db import modelsclass Book(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=32) price = models.DecimalField(max_digits=5, decimal_places=2) class Meta:  ordering=('id', ) # 默认以id排序

(2)进行数据库迁移

tool---->run manage.py task --- 执行两条命令->makemigrations---->migrate

(3)插入数据  views.py 

from django.shortcuts import render,HttpResponsefrom app01 import modelsdef index(request): #批量插入数据 #方式一 # for i in range(100): #  models.Book.objects.create(name='书籍%s'%i,price=i+1) #方式二 ll=[] for i in range(100):  book=models.Book(name='书籍%s'%i,price=i+1)  ll.append(book) #打印原生sql models.Book.objects.bulk_create(ll,10)#每次插入10条数据 return render(request,'index.html')

三、分页器的简单使用

1.需要提前知道的分页知识

from django.core.paginator import Paginatordef index(request): #######1 Paginator对象的属性和方法 book_list=models.Book.objects.all() # 实例化得到对象 # 第一个参数:要分页的数据,book_list # 第二个参数:每页条数 paginator=Paginator(book_list,10) # Paginator对象的属性和方法 print(paginator.per_page) # 每页显示的条数 print(paginator.count) # 总条数,总共要分页多少条数据 print(paginator.num_pages) # 总页码数 print(paginator.page_range) # 页码的生成器 [1,2,3,4,5,6,7,8,9,10]  ######3 Page对象的属性和方法 # Page类 的对象 page=paginator.page(2) # 第一页的对象 # 每一页的对象,属性和方法 print(page.has_next())  # 有没有下一页 print(page.next_page_number()) # 下一页页码 print(page.has_previous()) # 是否有上一页 print(page.previous_page_number()) # 上一页页面 (当前页如果是第一页,没有上一页) print(page.object_list)   # 当前页的所有数据 print(page.number)   # 当前页的页码数   #需要的第一个参数:页码的生成器 [1,2,3,4,5,6,7,8,9,10] page_range=paginator.page_range return render(request, 'index.html',locals())##### 4 表模型中默认以id排序 class Meta:  ordering=('id', ) # 默认以id排序

2.简单使用

views.py

from django.core.paginator import Paginatordef index(request): # 需要的第三个参数 page_num_int=int(request.GET.get('page',1)) book_list = models.Book.objects.all() paginator = Paginator(book_list, 10) # 需要的第一个参数:页码的生成器 [1,2,3,4,5,6,7,8,9,10] page_range = paginator.page_range # 需要的第二个参数,去到某一页的page对象 page = paginator.page(page_num_int) return render(request, 'index.html', {'page_range':page_range,'page':page,'page_num_int':page_num_int})

index.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://www.cnblogs.com//static/bootstrap/css/bootstrap.min.css"> <title>Title</title></head><body><div class="container-fluid"> <div class="row">  <div class="col-md-6 col-md-offset-3">   <table class="table">    <thead>    <tr>     <th>id</th>     <th>名字</th>     <th>价格</th>    </tr>    </thead>    <tbody>    {% for book in page.object_list %}     <tr>      <td>{{ book.id }}</td>      <td>{{ book.name }}</td>      <td>{{ book.price }}</td>     </tr>    {% endfor %}    </tbody>   </table>   <div class="text-center">    <nav aria-label="Page navigation">     <ul class="pagination">      {% if page.has_previous %}       <li>        <a href="https://www.cnblogs.com//?page={{ page.previous_page_number }}" aria-label="Previous">         <span aria-hidden="true">&laquo;</span>        </a>       </li>      {% else %}       <li class="disabled">        <a href="" aria-label="Previous">         <span aria-hidden="true">&laquo;</span>        </a>       </li>      {% endif %}      {% for page_num in page_range %}       {% if page_num_int == page_num %}        <li class="active"><a href="https://www.cnblogs.com//?page={{ page_num }}">{{ page_num }}</a></li>       {% else %}        <li><a href="https://www.cnblogs.com//?page={{ page_num }}">{{ page_num }}</a></li>       {% endif %}      {% endfor %}      {% if page.has_next %}       <li>        <a href="https://www.cnblogs.com//?page={{ page.next_page_number }}" aria-label="Next">         <span aria-hidden="true">&raquo;</span>        </a>       </li>      {% else %}       <li class="disabled">        <a href="" aria-label="Next">         <span aria-hidden="true">&raquo;</span>        </a>       </li>      {% endif %}     </ul>    </nav>   </div>  </div> </div></div></body></html>

url.py

from django.urls import pathfrom app01 import viewsurlpatterns = [ # path('admin/', admin.site.urls),  path('', views.index),]

 

三、分页器的进阶使用

1.需要提前知道的分页知识

# 最多显示前5 后5 和当前,总共11个页码,如果少于11,全部显示出来#逻辑分析  显示左5,右5,总共11个页, 1 如果总页码大于11  1.1 if 当前页码减5小于1,要生成1到12的列表(顾头不顾尾,共11个页码)   page_range=range(1,12)  1.2 elif 当前页码+5大于总页码,生成当前页码减10,到当前页码加1的列表(顾头不顾尾,共11个页码)   page_range=range(paginator.num_pages-10,paginator.num_pages+1)  1.3 else 生成当前页码-5,到当前页码+6的列表   page_range=range(current_page_num-5,current_page_num+6) 2 其它情况,生成的列表就是pageinator的page_range  page_range=paginator.page_range

2.进阶使用

views.py

### 完美的分页器def index(request): # 需要的第三个参数 page_num_int = int(request.GET.get('page', 1)) book_list = models.Book.objects.all() paginator = Paginator(book_list, 1) # 需要的第一个参数:页码的生成器 [1,2,3,4,5,6,7,8,9,10] # page_range = paginator.page_range if paginator.num_pages > 11:  # 当前条件符合了以后,有三种情况  if page_num_int - 5 < 1:   page_range = range(1, 11)  elif page_num_int + 5 > paginator.num_pages:   page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)  else:   page_range = range(page_num_int - 5, page_num_int + 5) else:  page_range = paginator.page_range # 需要的第二个参数,去到某一页的page对象 page = paginator.page(page_num_int) return render(request, 'index.html', {'page_range': page_range, 'page': page, 'page_num_int': page_num_int})

index.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://www.cnblogs.com//static/bootstrap/css/bootstrap.min.css"> <title>Title</title></head><body><div class="container-fluid"> <div class="row">  <div class="col-md-6 col-md-offset-3">   <table class="table">    <thead>    <tr>     <th>id</th>     <th>名字</th>     <th>价格</th>    </tr>    </thead>    <tbody>    {% for book in page.object_list %}     <tr>      <td>{{ book.id }}</td>      <td>{{ book.name }}</td>      <td>{{ book.price }}</td>     </tr>    {% endfor %}    </tbody>   </table>   <div class="text-center">    <nav aria-label="Page navigation">     <ul class="pagination">      {% if page.has_previous %}       <li>        <a href="https://www.cnblogs.com//?page={{ page.previous_page_number }}" aria-label="Previous">         <span aria-hidden="true">&laquo;</span>        </a>       </li>      {% else %}       <li class="disabled">        <a href="" aria-label="Previous">         <span aria-hidden="true">&laquo;</span>        </a>       </li>      {% endif %}      {% for page_num in page_range %}       {% if page_num_int == page_num %}        <li class="active"><a href="https://www.cnblogs.com//?page={{ page_num }}">{{ page_num }}</a></li>       {% else %}        <li><a href="https://www.cnblogs.com//?page={{ page_num }}">{{ page_num }}</a></li>       {% endif %}      {% endfor %}      {% if page.has_next %}       <li>        <a href="https://www.cnblogs.com//?page={{ page.next_page_number }}" aria-label="Next">         <span aria-hidden="true">&raquo;</span>        </a>       </li>      {% else %}       <li class="disabled">        <a href="" aria-label="Next">         <span aria-hidden="true">&raquo;</span>        </a>       </li>      {% endif %}     </ul>    </nav>   </div>  </div> </div></div></body></html>

 

原文转载:http://www.shaoqun.com/a/481389.html

西集网:https://www.ikjzd.com/w/1353

巨鲸:https://www.ikjzd.com/w/1986

急速:https://www.ikjzd.com/w/1861


一、分页器组件介绍1项目数据量大了以后,比如涉及到分页,一页一页的加载显示2django中分页器组件,把分页常用的东西,封装到一个类中3实例化得到一个对象,对象里有属性和方法二、模拟产生需要的数据这里自己造出来数据(1)造出表models.pyfromdjango.dbimportmodelsclassBook(models.Model):id=models.AutoField(primary_k
蜜芽宝贝:https://www.ikjzd.com/w/1320
拍拍:https://www.ikjzd.com/w/2205
海南官塘温泉好玩吗?有什么好玩的?:http://tour.shaoqun.com/a/5241.html
五一安徽天堂寨门票多少钱?2020五一六安天堂寨门票价格?:http://tour.shaoqun.com/a/46841.html
河源桂山大峡谷漂流门票是多少?:http://tour.shaoqun.com/a/2432.html

没有评论:

发表评论

注意:只有此博客的成员才能发布评论。