博客五部曲之三 - 博客RESTful


1357 浏览 5 years, 2 months

13 分页

版权声明: 转载请注明出处 http://www.codingsoho.com/

分页

这一节我们来讨论分页,同样会参考前面post_list的实现方法。
我们会讨论build-in的方案,以及自定义的方案两种

Pagination库

首先导入Pagination相关库,然在在pagination_class里定义

from rest_framework.pagination import (
    LimitOffsetPagination,
    PageNumberPagination,
    )

class PostListAPIView(ListAPIView):
    pagination_class = LimitOffsetPagination    

这里的limit就是每页显示的最大条目数,offset是起始条目

例如,访问http://127.0.0.1:8000/api/posts/?limit=2&offset=5, 表示每页最多显示2个,从第五个开始显示

也可以结果过滤一起操作,访问 http://127.0.0.1:8000/api/posts/?limit=2&offset=3&search=post

仅会显示包含post”的Post,总数从20降低为6个了。

这个是系统内嵌的Pagination,接下来我们打算自己来实现一个。

自定义pagination

定义文件

pagination.py

from rest_framework.pagination import (
    LimitOffsetPagination,
    PageNumberPagination,
    )

def PostLimitOffsetPagination(LimitOffsetPagination):
    default_limit  = 3
    max_limit = 10

在view里面添加这个类

class PostListAPIView(ListAPIView):
    pagination_class = PostLimitOffsetPagination

按理由到这个时候访问http://127.0.0.1:8000/api/posts时,不需要设定limit,默认会按default_limit分页,但实际上没工作,后面要查一下
设置设定limit都不工作

因为我们常用的是page访问的方式,所以我们实现该方式

def PostPageNumberPagination(PageNumberPagination):
    page_size = 4

这样,它会按每页4个条目显示

几种可工作情形

1.只设置settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 5
}

然后无参数访问 http://127.0.0.1:8000/api/comments

GET /api/comments/
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
    "count": 7,
    "next": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/?limit=5&offset=5",
    "previous": null,
    "results": [
        {
            "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/53",
            "id": 53,
            "content": "reply ++",
            "reply_count": 1
        },
        {

2.不设置settings.py,设置pagination_class

class CommentListAPIView(ListAPIView):
    pagination_class = LimitOffsetPagination

带参数访问可以分页http://127.0.0.1:8000/api/comments/?limit=3&offset=2 ,不带参数则不行

GET /api/comments/?limit=3&offset=2
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
    "count": 7,
    "next": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/?limit=3&offset=5",
    "previous": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/?limit=3",
    "results": [
        {
            "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/51",
            "id": 51,
            "content": "this is comment",
            "reply_count": 3
        },
        {
            "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/48",
            "id": 48,
            "content": "comments 2 on test",
            "reply_count": 2
        },
        {
            "url": "[http://127.0.0.1](http://127.0.0.1):8000/api/comments/47",
            "id": 47,
            "content": "comments on test",
            "reply_count": 0
        }
    ]
}

问题查出来了。。。

之前定义PostLimitOffsetPagination时用的是def,应该是class,这个python也没有检测出问题来??