Django入门到放弃之分页器

1.属性和方法

#######1 Paginator对象的属性和方法
    book_list=models.Book.objects.all() # 注意all操作实际是分批次获取数据,当前版本为limit 21
    # 实例化得到对象
    # 第一个参数:要分页的数据,book_list
    # 第二个参数:没有条数
    paginator=Paginator(book_list,10) # 分页的数据  每页展示的数据个数
    # Paginator对象的属性和方法
    print(paginator.per_page) # 每页显示的条数
    print(paginator.count) # 总条数,总共要分页多少条数据
    print(paginator.num_pages) # 总页码数
    print(paginator.page_range) # 页码数生成器 rang(1,总页数+1)
    
    
######3 Page对象的属性和方法
    # Page类 的对象  
    page=paginator.page(2)   #  第一页的对象
    
    # 每一页的对象,属性和方法
    print(page.count())      # 每一页的页码数
    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)           # 当前页的页码数

2.示例:  

urls.py

from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index,name='index'),
    path('test', views.test,name='test'),
]

view.py

from django.shortcuts import render, HttpResponse, reverse, redirect
from django.core.paginator import Paginator
from app01 import models


# Create your views here.

def index(request):
	# 设置跳转的页面,default=1为直接访问首页时初始化的值
    page_num_int = int(request.GET.get('page',default=1))  
    book_list = models.Book.objects.all()  # 注意all操作实际是分批次获取数据,当前版本为limit 21

    # 实例化对象
    # 设置每页展示的数据个数,默认为10
    per_page = int(request.GET.get('per_page',default=10))
    paginator = Paginator(book_list, per_page)  # 分页的数据  每页展示的数据个数

    # 如果总页数大于 展示个数+1 情况下,如果跳转的页面小于5则直接显示 1-10页,如果跳转的页面+5大于总页数则显示最后10页,否则显示当前页前5后5页
    # 如果总页数小于 展示个数+1 情况下,则展示全部页数
    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) # 页码数生成器 rang(1,总页数+1)
    else:
        page_range = paginator.page_range
    page_obj = paginator.page(page_num_int)


    return render(request, 'index.html', locals())

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/static/jquery-3.3.1/jquery-3.3.1.min.js"></script>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <style>
        # 设置placeholder字体颜色
        input::-webkit-input-placeholder{
            color:rgb(51, 122, 183);
        }
        input::-moz-placeholder{   /* Mozilla Firefox 19+ */
            color:rgb(51, 122, 183);
        }
        input:-moz-placeholder{    /* Mozilla Firefox 4 to 18 */
            color:rgb(51, 122, 183);
        }
        input:-ms-input-placeholder{  /* Internet Explorer 10-11 */
            color:rgb(51, 122, 183);
        }

        .pg {
            line-height: 26px;
        }

        #jump,#per_page_num {
            padding: -1px;
            margin: -1px;
            border: 1px solid rgb(221, 221, 221);
            width: 70px;
            line-height: 30px;
            font-size: 14px;
            color: rgb(51, 122, 183);
        }

        #x1,#x2 {
            padding: 0;
        }

    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>价格</th>
                </tr>
                </thead>
                <tbody>
                
                # 获取当前页所有数据,循环替换
                {% for page in page_obj.object_list %}
                    <tr>
                        <td>{{ page.id }}</td>
                        <td>{{ page.name }}</td>
                        <td>{{ page.price }}</td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>

            <div class="text-center">
                <nav aria-label="Page navigation">
                    <ul class="pagination pg">
                    # 如果当前页有上一页(跳转到首页),则跳转否则禁止点击
                        {% if page_obj.has_previous %}
                            <li>
                                {#    <a href="/?page={{ page_obj.previous_page_number }}" aria-label="Previous">#}
                                <a href="/?page=1&per_page={{ per_page }}" aria-label="Previous">
                                    <span aria-hidden="true">«</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="" aria-label="Previous">
                                    <span aria-hidden="true">«</span>
                                </a>
                            </li>
                        {% endif %}

					# 分页a标签添加跳转链接,当前页添加active属性
                        {% 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 }}&per_page={{ per_page }}">{{ page_num }}</a></li>
                            {% endif %}
                        {% endfor %}

						#如果不是最后一页则展示,否则不展示
                        {% if page_num_int !=  paginator.num_pages  %}
                             <li><a href="/?page={{ paginator.num_pages }}&per_page={{ per_page }}">... {{ paginator.num_pages }}</a></li>
                        {% endif %}

					# 如果有下一页则跳转,否则禁用
                        {% if page_obj.has_next %}
                            <li>
                                {#    <a href="/?page={{ page_obj.next_page_number }}" aria-label="Next">#}
                                <a href="/?page={{ paginator.num_pages }}&per_page={{ per_page }}" aria-label="Next">
                                    <span aria-hidden="true">»</span>
                                </a>
                            </li>
                        {% else %}
                            <li class="disabled">
                                <a href="" aria-label="Next">
                                    <span aria-hidden="true">»</span>
                                </a>
                            </li>
                        {% endif %}

						# 设置跳转
                        <li>
                               <span id="x1"><input type="text"   id="jump" size="2" title="输入页码,按回车快速跳转"   placeholder="{{ page_num_int }}/ {{ paginator.num_pages }} 页"
                                       onkeydown="if(event.keyCode===13) {window.location='?page='+this.value+'&per_page={{ per_page }}'; doane(event);}">
                                     </span>
                        </li>

						# 设置分页个数
                        <li>
                               <span id="x2">
                                   <input type="text"  id="per_page_num" size="2"  placeholder=" 条数:{{ per_page }}"
                                       onkeydown="if(event.keyCode===13) {window.location='?page='+ '{{ page_num_int }}&per_page=' + this.value; doane(event);}">
                               </span>
                        </li>
                    </ul>
                </nav>

            </div>
        </div>
    </div>
</div>

</body>
</html>

    

posted @ 2022-08-27 22:59  百衲本  阅读(23)  评论(0编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码