程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django rest framework---- paging

編輯:Python

Django REST Framework---- Pagination

  • One 、 function
  • 2. Global configuration
  • 3、 ... and 、 Custom distribution

One 、 function

Set the distribution and the number of pages

2. Global configuration

stay settting.py Add

REST_FRAMEWORK = {

'DEFAULT_PAGINATION_CLASS':[
'rest_framework.Pagination.PageNumberPaginaion',
'PAGE_SIZE':100,
# notes : The number of pages cannot be modified 
],
}

3、 ... and 、 Custom distribution

  1. Create a new paging class file (paginations.py)
from rest_framework.Pagination import PageNumberPagination
class Paging class name (PageNumberPagination):
page_size = 10 # The default is 10 strip 
page_query_param = 'page'
page_size_query_param = 'size'
max_page_size = 100 # At most... Can be set 100 strip 
  1. Add in view class
from .pagination import Paging class name
# Create your views here.
class Library name ViewSet(ModelViewSet):
...
pagination_class = Paging class name
  1. access
http://localhost:8000/api/students/?page=nu&size=nu

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved