restframework中提供了优秀的分页组件
在settings.py
中,填写关于restframework的配置
REST_FRAMEWORK = {
"UNAUTHENTICATED_USER": None,
"PAGE_SIZE": 4
}
1.PageNumberPagination
from rest_framework.pagination import LimitOffsetPagination
class BlogView(APIView):
authentication_classes = [BlogAuthentication, ]
def get(self, request, *args, **kwargs):
""" 博客列表 """
# 1.读取数据库中的博客信息
queryset = models.Blog.objects.all().order_by("id")
from rest_framework.pagination import LimitOffsetPagination
pager = LimitOffsetPagination()
res = pager.paginate_queryset(queryset, request, self)
# total_res = pager.get_paginated_response()
# 2.序列化
ser = BlogSerializers(instance=res, many=True)
response = pager.get_paginated_response(ser.data)
# 3.返回
return response
# 获取到所有queryset对象
queryset = models.Blog.objects.all().order_by("id")
from rest_framework.pagination import PageNumberPagination
# 实例化分页对象&传入参数(数据queryset,request对象,view)
pager = PageNumberPagination()
res = pager.paginate_queryset(queryset, request, self)
ser = BlogSerializers(instance=res, many=True)
response = pager.get_paginated_response(ser.data)
return response
return response
这里的response经过了get_paginated_response
方法的封装,具体过程:
class LimitOffsetPagination(BasePagination):
...
def get_paginated_response(self, data):
return Response(OrderedDict([
('count', self.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
get_paginated_response
在返回时调用了restframework
的response,
并且封装了更多的与分页相关的参数返回给调用它的API,
如果希望对封装的参数进行自定制,或者增加一些其他的参数,
可以写一个子类MyLimitOffsetPagination
并且
重写get_paginated_response(self, data)
方法
class MyLimitOffsetPagination(LimitOffsetPagination):
...
def get_paginated_response(self, data):
return Response(OrderedDict([
('xxx',self.xxx)
('count', self.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
{
"count": 15,
"next": "http://127.0.0.1:8000/api/blog/?limit=4&offset=4&page=2",
"previous": null,
"results": [
{
"id": 1,
"category": "云计算",
"image": "xxxx/xxxxx.png",
"title": "郑经理",
"summary": "....",
"ctime": "2022-10-01",
"comment_count": 0,
"favor_count": 0,
"creator": {
"id": 1,
"username": "wupeiqi"
}
},
{
"id": 2,
"category": "Python全栈",
"image": "xxxx/xxxxx.png",
"title": "震惊了",
"summary": "....",
"ctime": "2022-10-01",
"comment_count": 0,
"favor_count": 0,
"creator": {
"id": 2,
"username": "cxr"
}
},
......
]
}
2.LimitOffsetPagination
from rest_framework.pagination import LimitOffsetPagination
class BlogView(APIView):
authentication_classes = [BlogAuthentication, ]
def get(self, request, *args, **kwargs):
""" 博客列表 """
# 1.读取数据库中的博客信息
queryset = models.Blog.objects.all().order_by("id")
from rest_framework.pagination import LimitOffsetPagination
pager = LimitOffsetPagination()
res = pager.paginate_queryset(queryset, request, self)
# total_res = pager.get_paginated_response()
# 2.序列化
ser = BlogSerializers(instance=res, many=True)
response = pager.get_paginated_response(ser.data)
# 3.返回
return response
......
LimitOffsetPagination
更加适合滚动翻页的情况
LimitOffsetPagination,滚动翻页
/accounts/?offset=2&limit=10
/accounts/?offset=10&limit=10
/accounts/?lastid=10&offset=0&limit=10
/accounts/?lastid=20&offset=0&limit=10
原文地址:http://www.cnblogs.com/uichuan/p/16793819.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性