I'm currently trying to create an API that return list of objects with page and limit per page input from url parameter using django-rest-framework which i already done in my api view with custom Pagination
class PropertyListPagination(PageNumberPagination):
page_size = 20
page_size_query_param = 'page_size'
def get_paginated_response(self, data):
return Response({
'code': 200,
'data': data
})
@api_view(['GET'])
def property_list(request):
if request.method == 'GET':
paginator = PropertyListPagination()
queryset = Property.objects.all()
context = paginator.paginate_queryset(queryset, request)
serializer = PropertySerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
Currently if a page is out of range( for example if i have only 2 object and i set my url to page=3 and page_size=1 then it should out of range of total objects) then in the response it will return a 404 status and in the body:
{
"detail": "Invalid page."
}
Is there a way to customize for it to return 400 status and the following json body ?
{
"code": 400,
"error": "Page out of range"
}
Thank you