下载
pip install djangorestframework
models.py
class News(models.Model): title = models.CharField(verbose_name="标题", max_length=128) create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True) class Meta: verbose_name = "a. 新闻管理" verbose_name_plural = verbose_name ordering = ['-create_time']
views.py
from rest_framework import pagination class NewsPagination(pagination.CursorPagination): def get_next_link(self): try: link = super().get_next_link() # 自定义链接返回方式 if link: if link.find('127.0.0.1') < 0: return link.replace('http://', 'https://') return "" except: return "" # 页码的字段 cursor_query_param = 'page' page_size_query_param = 'size' # 每页多少条数据 page_size = 6 max_page_size = 200 # 排序方式 ordering = ['-create_time'] class NewsView(APIView): def get(self, request, *args, **kwargs): page_obj = NewsPagination() # 查询符合条件的数据 queryset = News.objects.all() # 如果有多个表共用,可以通过下面的方式使用每个表自定义的 ordering 排序 # page_obj.ordering = News._meta.ordering news_data = page_obj.paginate_queryset(queryset, request) # 下一页地址 next = page_obj.get_next_link() # 上一页地址 previous = page_obj.get_previous_link() return response_success({ 'next': next, 'previous': previous, 'news_data': news_data })
标签:pagination,obj,name,get,rest,next,link,Django,page From: https://www.cnblogs.com/chaoqi/p/17730935.html