我正在使用兩個輸入作為過濾器的搜索視圖:
- 搜索詞
2.城市(多選)
我使用序列化程式完成了它并且它作業但分頁不起作用,因為它是一個 post 方法,所以我試圖從 url 引數中獲取這些輸入,我嘗試了這種模式:
path(r"search/<str:search>/(?P<city>\w*)/",
SearchView.as_view({"get": "search"}), name="search"),
但是當我瀏覽時:http: //127.0.0.1 :8000/company/search/taxi/montrial/ 它回傳 Not Found: /company/search/ 那么如何傳遞引數或者是否有另一種方法可以通過 post 方法使用分頁
uj5u.com熱心網友回復:
我建議將分頁與 get 請求一起使用,或者如果您應該使用 post 進行分頁
class CustomPagination(pagination.PageNumberPagination):
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(), #you can read page number from url and put it here
'previous': self.get_previous_link()
},
'count': self.page.paginator.count,
'results': data
})
要從 url 讀取資料,您可以使用request.query_params
https://www.django-rest-framework.org/api-guide/pagination/
uj5u.com熱心網友回復:
我使用序列化器方法解決了它,繼承自具有分頁方法的 viewsets.GenericViewsets
class SearchView(viewsets.GenericViewSet):
permission_classes = [IsDriver]
queryset = CompanyProfile.objects.all()
serializer_class = SearchSerializer
@action(methods=['post'], detail=False)
def search(self, request, format=None):
serializer = SearchSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
page = self.paginate_queryset(serializer.data["companies"])
if page is not None:
# used CompanyProfileSerializer to serialize the companies query
serializer = CompanyProfileSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
return Response(serializer.data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517822.html
