首页 > 其他分享 >drf-分页器的使用

drf-分页器的使用

时间:2022-10-31 23:46:30浏览次数:100  
标签:00 12 分页 title price pub 使用 id drf

-----全局配置分页器

setting.py

REST_FRAMEWORK={
    #全局分页设置
    "DEFAULT_PAGINATION_CLASS":"rest_framework.pagination.PageNumberPagination",
    "PAGE_SIZE":10 #每页显示的个数
}

 

然后视图哪里进行查询接口返回

代码可以参考:

https://www.cnblogs.com/kaibindirver/p/13264461.html (原始的查询数据库方法)

https://www.cnblogs.com/kaibindirver/p/16833059.html (封装的见这个)

 

接口返回

{
    "count": 30,      #总数
    "next": "http://127.0.0.1:8000/book/?page=3",  #下一页的url
    "previous": "http://127.0.0.1:8000/book/",     #上一页的url
    "results": [
        {
            "id": 15,
            "title": "book10",
            "price": 19,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 16,
            "title": "book10",
            "price": 19,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 17,
            "title": "sanguo10",
            "price": 191,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 18,
            "title": "sanguo10",
            "price": 191,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 19,
            "title": "sanguo10",
            "price": 191,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 20,
            "title": "sanguo10",
            "price": 191,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 21,
            "title": "sanguo10",
            "price": 191,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 23,
            "title": "angu1o1",
            "price": 1911131,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 24,
            "title": "angu1o1",
            "price": 1911131,
            "pub_date": "2020-12-12 00:00:00"
        },
        {
            "id": 25,
            "title": "zzz",
            "price": 1212,
            "pub_date": "2022-12-01"
        }
    ]
}

 

标签:00,12,分页,title,price,pub,使用,id,drf
From: https://www.cnblogs.com/kaibindirver/p/16846340.html

相关文章