Для моего API я использую api_view decorator. Проблема с разбиением на страницы (ответ JSON). Я получил ответ без "блока нумерации страниц":
[
{
"id": 18,
"name": "Monitor Acer Test",
"price": "2212.00",
"stock": 21,
"image": "/media/9hq.webp",
"available": true
},
{
"id": 17,
"name": "Monitor LG Test",
"price": "2512.00",
"stock": 10,
"image": "/media/811AFxM28YL._SX425_.jpg",
"available": true
}
]
Я также попытался переопределить PageNumberPagination по умолчанию, он работает, но все еще без «блока разбиения на страницы»
Мой вид API (см. Пример GET> else):
@csrf_exempt
@api_view(['GET', 'POST',])
@permission_classes([AllowAny, ])
def product(request):
item = request.data
if request.method == 'POST':
serializer = ProductSerializer(data=item)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({"message": "Product created!", "data": request.data}, status=status.HTTP_201_CREATED)
else:
all_obj = Product.objects.filter(available=True)
if len(all_obj) > 0:
paginator = PageNumberPagination()
result_page = paginator.paginate_queryset(all_obj, request)
serializer = ProductSerializer(result_page, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response({"message": "There is no created items!"}, status=status.HTTP_200_OK)
Мои настройки.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',),
'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny',],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 2
}
Ожидаемый результат:
{
"count": 1023
"next": "https://api.example.org/accounts/?page=5",
"previous": "https://api.example.org/accounts/?page=3",
"results": [
…
]
}