• Django-10-分页组件


    1. Django内置分页

    from django.shortcuts import render
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    L = []
    for i in range(1, 999):
        L.append(i)
    
    
    # Create your views here.
    def index(request):
        current_page = request.GET.get('p')
        # paginator对象
        paginator = Paginator(L, 10)  # 10表示每页显示10条
        # per_page: 每页显示条目数量
        # count:    数据总个数
        # num_pages:总页数
        # page_range:总页数的索引范围,如: (1,10),(1,200)
        # page:     page对象
        try:
            # page对象
            posts = paginator.page(current_page)
            # has_next              是否有下一页
            # next_page_number      下一页页码
            # has_previous          是否有上一页
            # previous_page_number  上一页页码
            # object_list           分页之后的数据列表
            # number                当前页
            # paginator             paginator对象
        except PageNotAnInteger:
            posts = paginator.page(1)
        except EmptyPage:
            posts = paginator.page(paginator.num_pages)
        return render(request, 'index.html', locals())
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <ul>
            {% for item in posts.object_list %}
                <li>{{ item }}</li>
            {% endfor %}
        </ul>
        {% if posts.has_previous %}
            <a href="/index.html?p={{ posts.previous_page_number }}">上一页</a>
        {% else %}
            <a href="#">上一页</a>
        {% endif %}
        {% if posts.has_next %}
            <a href="/index.html?p={{ posts.next_page_number }}">下一页</a>
        {% else %}
            <a href="#">下一页</a>
        {% endif %}
        <span>{{ posts.number }}/{{ posts.paginator.num_pages }}</span>
    </body>
    </html>

    2. Django内置分页扩展

    class CustomPaginator(Paginator):                                                            
        def __init__(self, current_page=1, max_page_num=7, *args, **kwargs):                     
            super(CustomPaginator, self).__init__(*args, **kwargs)                               
            self.current_page = int(current_page)                                                
            self.max_page_num = max_page_num                                                     
                                                                                                 
        def page_num_range(self):                                                                
            # 总页数过少                                                                              
            if self.num_pages < self.max_page_num:                                               
                return range(1, self.num_pages + 1)                                              
            part = self.max_page_num // 2                                                        
            if self.current_page - part < 1:                                                     
                return range(1, self.max_page_num + 1)                                           
            if self.current_page + part > self.num_pages:                                        
                return range(self.num_pages + 1 - self.max_page_num, self.num_pages + 1)         
            return range(self.current_page - part, self.current_page + part + 1)                 
                                                                                                 
                                                                                                 
    def index2(request):                                                                         
        current_page = request.GET.get('p')                                                      
        try:                                                                                     
            # paginator对象                                                                        
            paginator = CustomPaginator(current_page, 11, L, 10)  # 10表示每页显示10条                  
            # per_page: 每页显示条目数量                                                                 
            # count:    数据总个数                                                                    
            # num_pages:总页数                                                                      
            # page_range:总页数的索引范围,如: (1,10),(1,200)                                              
            # page:     page对象                                                                   
        except Exception:                                                                        
            paginator = CustomPaginator(1, 11, L, 10)                                            
        try:                                                                                     
            # page对象                                                                             
            posts = paginator.page(current_page)                                                 
            # has_next              是否有下一页                                                       
            # next_page_number      下一页页码                                                        
            # has_previous          是否有上一页                                                       
            # previous_page_number  上一页页码                                                        
            # object_list           分页之后的数据列表                                                    
            # number                当前页                                                          
            # paginator             paginator对象                                                  
        except PageNotAnInteger:                                                                 
            posts = paginator.page(1)                                                            
        except EmptyPage:                                                                        
            posts = paginator.page(paginator.num_pages)                                          
        return render(request, 'index2.html', {'posts': posts})                                  
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <ul>
            {% for item in posts.object_list %}
                <li>{{ item }}</li>
            {% endfor %}
        </ul>
        {% if posts.has_previous %}
            <a href="/index2.html?p={{ posts.previous_page_number }}">上一页</a>
        {% else %}
            <a href="#">上一页</a>
        {% endif %}
        {% for i in posts.paginator.page_num_range %}
            <a href="/index2.html?p={{ i }}">{{ i }}</a>
        {% endfor %}
        {% if posts.has_next %}
            <a href="/index2.html?p={{ posts.next_page_number }}">下一页</a>
        {% else %}
            <a href="#">下一页</a>
        {% endif %}
        <span>{{ posts.number }}/{{ posts.paginator.num_pages }}</span>
    </body>
    </html>

    3. 自定义分页

    class Paginator(object):
        def __init__(self, total_count, current_page, per_page_item_num=10, max_page_num=7):
            # 数据总个数
            self.total_count = total_count
            # 当前页
            try:
                v = int(current_page)
                if v <= 0:
                    v = 1
                self.current_page = v
            except Exception:
                self.current_page = 1
            # 每页显示的行数
            self.per_page_item_num = per_page_item_num
            # 最多显示页面
            self.max_page_num = max_page_num
    
        def start(self):
            return (self.current_page - 1) * self.per_page_item_num
    
        def end(self):
            return self.current_page * self.per_page_item_num
    
        @property
        def num_pages(self):
            """
            总页数
            :return:
            """
            # 666
            # 10
            a, b = divmod(self.total_count, self.per_page_item_num)
            if b == 0:
                return a
            return a + 1
    
        def page_num_range(self):
            if self.num_pages < self.max_page_num:
                return range(1, self.num_pages + 1)
            part = self.max_page_num // 2
            if self.current_page - part < 1:
                return range(1, self.max_page_num + 1)
            if self.current_page + part > self.num_pages:
                return range(self.num_pages + 1 - self.max_page_num, self.num_pages + 1)
            return range(self.current_page - part, self.current_page + part + 1)
    
        def page_str(self):
            page_list = []
    
            first = "<li><a href='/index3.html?p=1'>首页</a></li>"
            page_list.append(first)
    
            if self.current_page == 1:
                prev = "<li><a href='#'>上一页</a></li>"
            else:
                prev = "<li><a href='/index3.html?p=%s'>上一页</a></li>" % (self.current_page - 1,)
            page_list.append(prev)
            for i in self.page_num_range():
                if i == self.current_page:
                    temp = "<li class='active'><a href='/index3.html?p=%s'>%s</a></li>" % (i, i)
                else:
                    temp = "<li><a href='/index3.html?p=%s'>%s</a></li>" % (i, i)
                page_list.append(temp)
    
            if self.current_page == self.num_pages:
                nex = "<li><a href='#'>下一页</a></li>"
            else:
                nex = "<li><a href='/index3.html?p=%s'>下一页</a></li>" % (self.current_page + 1,)
            page_list.append(nex)
    
            last = "<li><a href='/index3.html?p=%s'>尾页</a></li>" % (self.num_pages,)
            page_list.append(last)
    
            return ''.join(page_list)
    def index3(request):
        from app01.pager import Paginator
        current_page = request.GET.get('p')
        page_obj = Paginator(666, current_page)
        data = L[page_obj.start():page_obj.end()]
        return render(request, 'index3.html', locals())
    <!DOCTYPE html>
    {% load staticfiles %}
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="{% static 'plugin/bootstrap/css/bootstrap.css' %}">
    </head>
    <body>
    <ul>
        {% for row in data %}
            <li>{{ row }}</li>
        {% endfor %}
    </ul>
    
    <ul class="pagination pagination-sm">
                {{ page_obj.page_str|safe }}
    </ul>
    <div style="height: 300px;"></div>
    </body>
    </html>
    
  • 相关阅读:
    linux shell基本知识
    chkconfig命令 centos 开机启动命令
    centos 7修改网卡名称
    centos 系统安装基本步骤,面试必考
    nginx 服务脚本编写模板
    nginx 服务脚本编写模板
    Mysql 多实例实施步骤
    shell常用监控脚本
    nginx做负载均衡 tomcat获得客户端真实ip
    vmvare安装系统提示vmci.sys 版本不正确解决方法
  • 原文地址:https://www.cnblogs.com/lsf123456/p/11465059.html
Copyright © 2020-2023  润新知