zoukankan      html  css  js  c++  java
  • [oldboy-django][2深入django]分页功能

    1 django自带分页

      1.1 分页模板

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
    </head>
    <body>
        <p>用户信息</p>
        <table  class="table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>age</th>
                    <th>type</th>
                </tr>
            </thead>
            <tbody>
                {% for row in current_page_obj %}
                    <tr>
                        <td>{{ row.id }}</td>
                        <td>{{ row.name }}</td>
                        <td>{{ row.age }}</td>
                        <td>{{ row.ut.title }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
        <div>
    
            {% if current_page_obj.has_previous %}
                <a href="/fenye/?page={{ current_page_obj.previous_page_number }}">上一页</a>
            {% endif %}
            {% for page in current_page_obj.paginator.page_range %}
                <a href="/fenye/?page={{ page }}">{{ page }}</a>
            {% endfor %}
    
            {% if current_page_obj.has_next %}
                <a href="/fenye/?page={{ current_page_obj.next_page_number }}">下一页</a>
            {% endif %}
    
    
    
        </div>
    </body>
    </html>
    View Code

      1.2 分页视图函数

    def fenye(request):
    
        user_list = models.UserInfo.objects.all()
        # 实现分页功能
        from django.core.paginator import Page,Paginator
    
        current_page_number = request.GET.get('page')
        paginator = Paginator(user_list, 10)
                # print(paginator.count)            #数据总行数 305
                # print(paginator.num_pages)        #总页数 31
                # print(paginator.object_list)      #当前页的数据 QuerySet[obj,obj]
                # print(paginator.per_page)         #每一页显示多少行数据10
                # print(paginator.page_range)       #页数的范围(1,32), 是一个range(1,32),即是一个数组
                # print(paginator.page(1))          #里面的1表示页码数,
        try:
            current_page_obj = paginator.page(current_page_number) # 也是一个QuerySet对象 [obj,obj,..]
        except Exception as e:
            current_page_obj = paginator.page(1)
    
                # print(current_page_obj.has_next())          #当前页是否有下一页
                # print(current_page_obj.next_page_number())  #当前页的下一页码
                # print(current_page_obj.has_previous())      # 当前页是否有上一页
                # print(current_page_obj.previous_page_number())# 当前页的上一页码
                # print(current_page_obj.object_list)         # 当前页的数据行列表QuerySet[obj,obj,...]
                # print(current_page_obj.number)              # 当前页码
                # print(current_page_obj.paginator)           # 当前页的paginator对象
    
        return render(request, 'fenye.html', {'current_page_obj': current_page_obj})
    View Code

     

    2 自定义分页

      2.1 模板

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css">
    </head>
    <body>
        <p>用户信息</p>
        <table  class="table">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>age</th>
                    <th>type</th>
                </tr>
            </thead>
            <tbody>
                {% for row in user_list %}
                    <tr>
                        <td>{{ row.id }}</td>
                        <td>{{ row.name }}</td>
                        <td>{{ row.age }}</td>
                        <td>{{ row.ut.title }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    
    {#    {{ page_info.pager|safe }}#}
    {#利用了bootstrap的分页组件#}
        
        
        <nav aria-label="Page navigation">
            <ul class="pagination">
                {{ page_info.pager|safe }}
            </ul>
        </nav>
    
    
    </body>
    </html>
    View Code

      2.2 自定义模块

    class PageInfo(object):
        def __init__(self, current_page_num, all_count, base_url, per_page=10, show_page=11):
            try:
                self.current_page_num = int(current_page_num)
            except Exception as e:
                self.current_page_num = 1
            self.per_page = per_page
            a,b = divmod(all_count, per_page)
            if b:
                a = a + 11
    
            self.all_page = a
            self.show_page = show_page
            self.base_url = base_url
    
    
        def start(self):
            return (self.current_page_num - 1) * self.per_page
    
        def end(self):
            return self.current_page_num * self.per_page
    
        def pager(self):
    
            page_list = []
            # 如果总的页码数小于11
            if self.show_page >= self.all_page:
                begin = 1
                stop = self.all_page
            # 总页码数大于11
            else:
                half,_ = divmod(self.show_page,2)
                if self.current_page_num <= half:
                    begin = 1
                    stop = self.show_page
                else:
                    temp = self.all_page - half
                    if self.current_page_num >= temp:
                        begin = self.all_page - self.show_page + 1
                        stop = self.all_page
                    else:
                        begin = self.current_page_num - half
                        stop = self.current_page_num + half
    
            prev_page = ''
            if self.current_page_num == 1:
                prev_page = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'
            else:
                prev_page = '<li><a href="%s?page=%s" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'% (self.base_url, self.current_page_num -1)
            page_list.append(prev_page)
            for i in range(begin, stop + 1):
                if i == self.current_page_num:
                    temp = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url, i,i)
                else:
                    temp = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i,i)
                page_list.append(temp)
    
            next_page= ''
            if self.current_page_num == self.all_page:
                next_page = '<li class="disabled"><a aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'
            else:
                next_page = '<li><a href="%s?page=%s" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>' % (self.base_url,self.current_page_num +1)
            page_list.append(next_page)
            return ''.join(page_list)
    View Code

      2.3 视图

    def custom_fenye(request):
        from utils.pagation_define import PageInfo
        current_page_number = request.GET.get('page')
        all_count = models.UserInfo.objects.all().count()
        page_info = PageInfo(current_page_number, all_count, "/custom_fenye.html/")
        user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]  # 列表切片操作 a[start:end]   start<= index < end
        return render(request, 'custom_fenye.html', {'user_list': user_list, 'page_info': page_info})
    View Code
  • 相关阅读:
    Beta 阶段计划
    alpha阶段总结
    冲刺第十天 12.6 THU
    冲刺第九天 12.5 WED
    冲刺第八天 12.4 TUE
    冲刺第七天 12.3 MON
    冲刺第六天 11.30 FRI
    正弦交流电有效值系数sqrt(2)的推导
    关于STM32官方FOC库函数扇区分析中’131072’系数的解释
    闭环系统零、极点位置对时间响应性能指标的影响
  • 原文地址:https://www.cnblogs.com/liuzhipenglove/p/7777560.html
Copyright © 2011-2022 走看看