如何使用 Django 进行分页

Django自带的分页插件

    现象这样的一个问题  ,分页是不是有规律的,是个什么样的规律?,

    假如我们对每页的数据固定显示20条  是什么样的呢?

    pageSize=20 每页多少条

    page=7 页码

每页条数(pageSize)

页码

每页数据

10

1

0-10

10

2

10-20

10

7

60-70

10

n

(N-1)*pageSize-n*pageSize

                  是不是这样?这就是最基本的分页方式

         最基本的分页思路

def list_students(request):
    if request.method == "GET":
        page = request.GET.get("page") #获取页码
        page_size = request.GET.get("page_size") #获取每页条数
        if not page: #设置默认页码为1
            page = 1
        if not page_size: #设置默认每页5
            page_size = 5
        page = int(page) #防止url直接传过来字符串的页码
        page_size = int(page_size) #防止url直接传过来字符串的页码
        start = (page-1)*page_size #页码数据开头的索引
        end = page*page_size  #页码数据结尾的索引
    students = Student.objects.order_by("id") #排序查询
    lenth = len(students) #获取长度
    page_num = lenth/page_size #计算页码范围
    if page_num != int(page_num): #判断页码是否为整数
        page_num += 1
    page_num = int(page_num)
    page_range = range(1,page_num+1) #页码范围
    data = students[start:end] #截取数据
    return render(request,"stu_list.html",{“data”:data,”page_range”:page_range})

使用django的分页插件进行分页

1、查询所有的数据

2、将数据传入分页的函数

3、调用具体页的数据

from django.core.paginator import Paginator

def list_students(request):
    if request.method == "GET":
        page = request.GET.get("page")
        page_size = request.GET.get("page_size")
        if not page:
            page = 1
        if not page_size:
            page_size = 5
        page = int(page)
        all_data = Student.objects.all() #查询所有的数据 --->1<---
        all_page = Paginator(all_data,int(page_size)) #将数据和单页条数放到Paginator里面  all_data = Student.objects.all() #查询所有的数据 --->2<---

        data = all_page.page(page) #获取具体页的数据 --->3<---
    return render(request,"stu_list.html",locals())
 

#基于分页实例
<P>
    <p>
        +++++++++++++++++++++总条数数+++++++++++++++++++++++++++
        <br>{{ all_page.count }}
    </p>


    <p>
        +++++++++++++++++++++总页数+++++++++++++++++++++++++++
        <br>{{ all_page.num_pages }}
    </p>

    <p>
        +++++++++++++++++++++页码范围+++++++++++++++++++++++++++
        <br>{{ all_page.page_range }}
    </p>
</P>
#基于当前分页
<p>
    <p>
        +++++++++++++++++++++当前页具体数据+++++++++++++++++++++++++++
        <br>{{ data.object_list }}
    </p>
    <p>
        +++++++++++++++++++++当前页码+++++++++++++++++++++++++++
        <br>{{ data.number }}
    </p>
    <p>
        +++++++++++++++++++++当前是否有上一页+++++++++++++++++++++++++++
        <br>{{ data.has_previous }}<br>
        +++++++++++++++++++++当前是否有下一页+++++++++++++++++++++++++++
        <br>{{ data.has_other_pages }}
    </p>
    <p>
        +++++++++++++++++++++开始索引+++++++++++++++++++++++++++
        <br>{{ data.start_index }}<br>
        +++++++++++++++++++++结束索引+++++++++++++++++++++++++++
        <br>{{ data.end_index }}
    </p>
</p>

高级分页思路

Django查询本身是有惰性的,all并不是将所有的数据查询出来,只有在排序或者截取的时候,才会 具体查询。

查询 第1页到第5页,数据库只查询前50条数据,

查询 第6页的时候,会查询50-100。

首先编写实例代码

"""

page_size = 10

当我们查询第1-5页的时候,不对所有数据进行查询,只查询前50条,也就是5页

#page 1 select_range 0-50 return_range 1-10 r_n = 1 s_n = 1

#page 2 select_range 0-50 return_range 10-20 r_n = 2 s_n = 1

#page 3 select_range 0-50 return_range 20-30 r_n = 3 s_n = 1

#page 4 select_range 0-50 return_range 30-40 r_n = 4 s_n = 1

#page 5 select_range 0-50 return_range 40-50 r_n = 5 s_n = 1

#page 6 select_range 50-100 return_range 0-10 r_n = 1 s_n = 2

#page 7 select_range 50-100 return_range 10-20 r_n = 2 s_n = 2

#page 8 select_range 50-100 return_range 20-30 r_n = 3 s_n = 2

#page 9 select_range 50-100 return_range 30-40 r_n = 4 s_n = 2

#page n (s_n-1)*50-s_n*50

"""

total = list(range(103))

while True:
    page = int(input(">>>"))

    if page%5 == 0:
        s_n = int(page/5)
        r_n = 5
    else:
        s_n = int(page / 5)+1
        r_n = page%5
    select_start = (s_n-1)*50
    select_end = s_n*50
    select_range = total[select_start:select_end]
    return_start = (r_n-1)*10
    return_end = r_n*10
    return_range = select_range[return_start:return_end]

    page_range = [3,4,5,6,7]
    if page <= 3:
        page_range = range(1,6)
    else:
        page_range = range(page-2,page+3)

    print("+++++++++++++++++++++++++++++++++++++++++++++++++++")
    print("当前的页码是:%s" % page)
    print("当前的大页码(s_n)是:%s" % s_n)
    print("当前的小页码是(r_n):%s" % r_n)
    print("查询的大范围(50条范围是):%s" % str(select_range))
    print("查询的小范围(10条范围是):%s" % str(return_range))
    print("反馈的页码是 :%s" % str(list(page_range)))
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++")

然后将代码嵌入到我们的查询试图当中

def list_students(request):
    if request.method == "GET":
        page = request.GET.get("page")
        page_size = request.GET.get("page_size")
        onece_page = 5
        if not page:
            page = 1
        if not page_size:
            page_size = 5
        page = int(page)
        page_size =int(page_size)
        if page % onece_page == 0:
            s_n = int(page / onece_page)
            r_n = onece_page
        else:
            s_n = int(page / onece_page) + 1
            r_n = page % onece_page

        total = Student.objects.all()  # 查询所有的数据

        select_start = (s_n - 1) * onece_page*page_size
        select_end = s_n * onece_page*page_size
        select_range = total[select_start:select_end]
        return_start = (r_n - 1) * page_size
        return_end = r_n * page_size
        return_range = select_range[return_start:return_end]

        if page <= 3:
            page_range = range(1, 6)
        else:
            page_range = range(page - 2, page + 3)

    return render(request,"stu_list.html",{"data":return_range,"page_range":page_range})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值