• Django——完美的分页器


    一、分页器组件介绍

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

    二、模拟产生需要的数据

    这里自己造出来数据

     (1)造出表  models.py   

    from django.db import models
    
    
    class 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,HttpResponse
    from app01 import models
    
    def 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 Paginator
    def 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 Paginator
    
    def 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="/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="/?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="/?page={{ page_num }}">{{ page_num }}</a></li>
                                {% else %}
                                    <li><a href="/?page={{ page_num }}">{{ page_num }}</a></li>
                                {% endif %}
    
                            {% endfor %}
    
    
                            {% if page.has_next %}
                                <li>
                                    <a href="/?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 path
    from app01 import views
    urlpatterns = [
        #    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="/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="/?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="/?page={{ page_num }}">{{ page_num }}</a></li>
                                {% else %}
                                    <li><a href="/?page={{ page_num }}">{{ page_num }}</a></li>
                                {% endif %}
    
                            {% endfor %}
    
    
                            {% if page.has_next %}
                                <li>
                                    <a href="/?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>
  • 相关阅读:
    jsp get参数乱码问题
    oracle 列相减——(Oracle分析函数Lead(),Lag())
    js获取本机id
    java mar --->JSONArray.fromObject
    五级菜单
    15 Spring Boot Shiro 验证码
    13 Spring Boot Shiro使用JS-CSS-IMG
    8:Spring Boot中thymeleaf模板中使用 Shiro标签
    8:Spring Boot Shiro记住密码
    阿里巴巴的阿里云中央仓库
  • 原文地址:https://www.cnblogs.com/guojieying/p/13845787.html
Copyright © 2020-2023  润新知