您的位置:首页 > 健康 > 养生 > 长沙景点一日游排行榜_免费交友软件_广州网站设计制作_seo免费优化软件

长沙景点一日游排行榜_免费交友软件_广州网站设计制作_seo免费优化软件

2024/12/21 21:40:56 来源:https://blog.csdn.net/qq_29518275/article/details/142550968  浏览:    关键词:长沙景点一日游排行榜_免费交友软件_广州网站设计制作_seo免费优化软件
长沙景点一日游排行榜_免费交友软件_广州网站设计制作_seo免费优化软件

自定义分页器代码:

from django.core.paginator import InvalidPage
from django.core.paginator import Paginator as DjangoPaginator
from django.utils.functional import cached_property
from rest_framework.exceptions import NotFound
from rest_framework.pagination import (PageNumberPagination,_get_displayed_page_numbers,_get_page_links,_positive_int,
)
from rest_framework.response import Response
from rest_framework.utils.urls import remove_query_param, replace_query_paramclass DataPaginator(DjangoPaginator):def validate_number(self, number):"""覆盖默认方法,去掉最大无效page校验:param number::return:"""try:if isinstance(number, float) and not number.is_integer():raise ValueErrornumber = int(number)except (TypeError, ValueError):return super(DataPaginator, self).validate_number(number)if number < 1:return super(DataPaginator, self).validate_number(number)return numberdef page(self, number):if getattr(self, "_page_enabled", True):return super(DataPaginator, self).page(number)else:# 返回所有的数据return self._get_page(self.object_list[:], number, self)class DataPageNumberPagination(PageNumberPagination):page_size = 10page_query_param = "page"page_size_query_param = "page_size"max_page_size = 1000django_paginator_class = DataPaginatordef get_paginated_response(self, data):return Response({"count": self.page.paginator.count, "results": data})def get_html_context(self):"""去除默认最大页限制:return:"""base_url = self.request.build_absolute_uri()def page_number_to_url(page_number):if page_number == 1:return remove_query_param(base_url, self.page_query_param)else:return replace_query_param(base_url, self.page_query_param, page_number)current = self.page.numberfinal = self.page.paginator.num_pagesif current < final:page_numbers = _get_displayed_page_numbers(current, final)else:page_numbers = _get_displayed_page_numbers(final, final)page_links = _get_page_links(page_numbers, current, page_number_to_url)return {"previous_url": self.get_previous_link(),"next_url": self.get_next_link(),"page_links": page_links,}def paginate_queryset(self, queryset, request, view=None):"""覆盖默认方法,从 self.page_params 中获取参数:param queryset::param request::param view::return:"""self.request = requestpage_size = self.get_page_size(request)if not page_size:return Nonepaginator = self.django_paginator_class(queryset, page_size)# 默认为Truepage_enabled = self.page_params.get("page_enabled", True)if page_enabled == "false":page_enabled = Falsesetattr(paginator, "_page_enabled", page_enabled)page_number = self.page_params.get(self.page_query_param, 1)if page_number in self.last_page_strings:page_number = paginator.num_pagestry:self.page = paginator.page(page_number)except InvalidPage as exc:msg = self.invalid_page_message.format(page_number=page_number, message=str(exc))raise NotFound(msg)if paginator.num_pages > 1 and self.template is not None:# The browsable API should display pagination controls.self.display_page_controls = Truereturn list(self.page)@cached_propertydef page_params(self):"""获取分页的参数:return:"""if self.request.method == "POST":return self.request.datareturn self.request.query_paramsdef get_page_size(self, request):"""覆盖默认方法,从 self.page_params 中获取参数:param request::return:"""if self.page_size_query_param:try:return _positive_int(self.page_params[self.page_size_query_param],strict=True,cutoff=self.max_page_size,)except (KeyError, ValueError):passreturn self.page_size

在drf的视图中使用:

from common.drf.page import DataPageNumberPaginationclass ScanRecordList(mixins.ListModelMixin,generics.GenericAPIView):queryset = models.ScanRecord.objects.all()serializer_class = serializers.ScanRecordSerializerpagination_class = DataPageNumberPagination  # 这里指定分页器def get(self, request, *args, **kwargs):return self.list(request, *args, **kwargs)

前端调用:

page=2&page_enabled=false&page_size=10

Tips:

page_enabled为false,会返回全部数据。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com