Django_rest-framework pagination

tags: Django

Configure settings, global configuration paging

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':  'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10  # Number of pages
}

Custom Pagination class

from collections import OrderedDict

from django.core.paginator import InvalidPage
from rest_framework.exceptions import NotFound
from rest_framework.pagination import PageNumberPagination

#http://127.0.0.1:9008/book/?page=1&page_size=6
from rest_framework.response import Response


class BookListPager(PageNumberPagination):
    page_size = 5  # 5 records per page
    page_size_query_param = 'page_size'  # Parameter name of the number of records per page


    # Custom pagination form
    def get_paginated_response(self, data):
        return Response(OrderedDict([
            ('count', self.page.paginator.count),
            ('page_range', list(self.page.paginator.page_range)),  # Page range
            ('has_next', self.page.has_next()),
            ('has_prious', self.page.has_previous()),
            ('next_page_number', self.page.next_page_number()),
            ('results', data)
        ]))

Note: If you turn off the paging function in the view, just set it in the view

pagination_class = None

Optional pager

1> PageNumberPagination

Front-end access URL form:

GET  http://api.example.org/books/?page=4

Properties that can be defined in subclasses:
page_size per page
page_query_param The page keyword name sent by the front end, the default is "page"
page_size_query_param The keyword name per page sent by the front end, the default is None
max_page_size The maximum number per page that can be set by the front end

from rest_framework.pagination import PageNumberPagination
    class StandardPageNumberPagination(PageNumberPagination):    page_size_query_param = 'page_size'

    max_page_size = 10

    class BookListView(ListAPIView):    queryset = BookInfo.objects.all().order_by('id')

    serializer_class = BookInfoSerializer
    pagination_class = StandardPageNumberPagination
    # 127.0.0.1/books/?page=1&page_size=2

2> LimitOffsetPagination

Front-end access URL form:

GET http://api.example.org/books/?limit=100&offset=400

Properties that can be defined in subclasses:
default_limit The default limit, the default value is the same as the PAGE_SIZE setting
limit_query_paramlimit parameter name, default ‘limit’
offset_query_param offset parameter name, default ’offset’
max_limit The maximum limit, the default is None

from rest_framework.pagination import LimitOffsetPagination
    class BookListView(ListAPIView):    queryset = BookInfo.objects.all().order_by('id')

    serializer_class = BookInfoSerializer
    pagination_class = LimitOffsetPagination
    # 127.0.0.1:8000/books/?offset=3&limit=2

Intelligent Recommendation

DRF Framework Summary - Pagination

Pagination Pagination The REST framework provides paging support. We can set the global paging mode in the configuration file, such as: You can also add different paging behaviors to your views by cus...

Pagination of the FLASK framework

Directly dry goods This is a paged view function @app.route(’/stupage’) def stu_page(): Look at the template {% for i in stus %} loops out the data of the current page tr td {{i.id}} /td&g...

SSH framework pagination

DaoImp ​​inside BIz layer BIzImpl layer Action control layer Reception parameters distal div In the front javascript Note: 1, in order to implement paging body which should form the inside of the body...

django rest framework pagination

REST framework includes support for customizable pagination style. This gives you large result sets can be divided into separate data page. Page API supports: In the form of a link as part of the pagi...

Django-Rest-Framework pagination

Django-Rest-Framework pagination  restful framework DRF pagination Why use paged DRF provides three pagination Global Configuration The first PageNumberPagination to page n, page n data show http...

More Recommendation

Use of pagination for CodeIgniter framework

php code part Front-end HTML part It should be noted that the introduction of external files is two configurations of the ci framework autoload.php file $autoload[‘helper’] = array(‘...

Angularjs framework integrated pagination

1. Install the plug-in (reference:https://github.com/angular-ui/bootstrap) 1. First integratebootstrap.min.css, download link:https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css 2. Second in...

Django framework learning pagination

First introduce the modules required by django from django.shortcuts import render,redirect,reverse Then import the paging module from django.core.paginator import Paginator, EmptyPage, PageNotAnInteg...

Django framework pagination

1. Built-in paging 1. Paging principle: 2. Grammar: Django's built-in paging function can only complete the "previous page" and "next page" operations. If other functions are neede...

[Jpa framework implements pagination] jpa implements pagination...

Table of contents Preface: Console layer: Pagination tool class: Preface: I just took notes, didn’t have much time to write in detail, and I was too busy moving bricks! If you have any questions...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top