<div id="cnblogs_post_body" class="blogpost-body">
一、分页
分页(如:在url后面加上?offset=0&limit=2,即代表从第0条开始,往后取2条(即1,2))



<span style="color: #0000ff;">class<span style="color: #000000;"> P1(LimitOffsetPagination):
max_limit = 3 <span style="color: #008000;">#<span style="color: #008000;"> 最大显示3条数据,当取的条数超过3条时,只显示前3条,不会再显示后面的,这个值默认是None,即不做限制
default_limit =2 <span style="color: #008000;">#<span style="color: #008000;"> 设置每一页显示多少条
limit_query_param = <span style="color: #800000;">'<span style="color: #800000;">limit<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 往后取几条
offset_query_param = <span style="color: #800000;">'<span style="color: #800000;">offset<span style="color: #800000;">' <span style="color: #008000;">#<span style="color: #008000;"> 当前所在的位置
<span style="color: #0000ff;">class<span style="color: #000000;"> IndexView2(APIView):
<span style="color: #008000;">#<span style="color: #008000;">使用http://127.0.0.1:8080/app01/v1/index2/?offset=2&limit=4可进行判断
<span style="color: #0000ff;">def get(self,request,*args,**<span style="color: #000000;">kwargs):
user_list =<span style="color: #000000;"> models.UserInfo.objects.all()
p1 = P1()<span style="color: #008000;">#<span style="color: #008000;">注册分页
page_user_list = p1.paginate_queryset(queryset=user_list,request=request,view=<span style="color: #000000;">self)
<span style="color: #0000ff;">print(<span style="color: #800000;">'<span style="color: #800000;">打印的是分页的数据<span style="color: #800000;">'<span style="color: #000000;">,page_user_list)
ser = MySerializes(instance=page_user_list,many=True) <span style="color: #008000;">#<span style="color: #008000;">可允许多个
<span style="color: #008000;">#<span style="color: #008000;"> return Response(ser.data) #不含上一页下一页
<span style="color: #0000ff;">return<span style="color: #000000;"> p1.get_paginated_response(ser.data)
=======================也可以用下面这种形式===========
<span style="color: #0000ff;">class<span style="color: #000000;"> BaseResponse(object):
<span style="color: #0000ff;">def <span style="color: #800080;">init(self,code=1000,data=None,error=<span style="color: #000000;">None):
self.code =<span style="color: #000000;"> code
self.data =<span style="color: #000000;"> data
self.error =<span style="color: #000000;"> error
<span style="color: #0000ff;">class<span style="color: #000000;"> IndexView(views.APIView):
<span style="color: #800000;">'''<span style="color: #800000;">第二种类表示的方式<span style="color: #800000;">'''
<span style="color: #0000ff;">def get(self,**<span style="color: #000000;">kwargs):
ret =<span style="color: #000000;"> BaseResponse()
<span style="color: #0000ff;">try<span style="color: #000000;">:
user_list =<span style="color: #000000;"> models.UserInfo.objects.all()
p1 =<span style="color: #000000;"> P1()
page_user_list = p1.paginate_queryset(queryset=user_list,view=<span style="color: #000000;">self)
ser = IndexSerializer(instance=page_user_list,many=<span style="color: #000000;">True)
ret.data =<span style="color: #000000;"> ser.data
ret.next = p1.get_next_link() <span style="color: #008000;">#<span style="color: #008000;">下一页链接
<span style="color: #0000ff;">except<span style="color: #000000;"> Exception as e:
ret.code= 1001<span style="color: #000000;">
ret.error = <span style="color: #800000;">'<span style="color: #800000;">xxxx错误<span style="color: #800000;">'
<span style="color: #0000ff;">return Response(ret.<span style="color: #800080;">dict)
<p class="secondtitle">2、基于页码的分页
<div class="cnblogs_code">
rest_framework.pagination PageNumberPagination