I have written a code in Python using Django Rest Framework to fetch data from database(MySql) and view it in browser using Pagination(LimitOffSetPagination
).But It is not working fine.But when I used PageNumberPagination
it was working great. so I am posting my new code.Commented parts of my code is actually for PageNumberPagination
. So just ignore it. Please tell me the necessary changes needed and help me out. The error coming is:
Exception Value: index() missing 1 required positional argument: 'request'
view.py
from rest_framework.pagination import PageNumberPagination
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from rest_framework.pagination import LimitOffsetPagination
@api_view(['GET','POST'])
def index(self, request):
if request.method=='GET':
all_dataobj=fetchdata.objects.all()
page = self.paginate_queryset(all_dataobj)
if page is not None:
pserializer = self.get_fetchdataSerializers(page, many=True)
return self.get_paginated_response(pserializer.data)
pserializer = self.get_fetchdataSerializers(all_dataobj, many=True)
return Response(pserializer.data)
# paginator = StandardResultsSetPagination()
# result_page = paginator.paginate_queryset(all_dataobj, request)
# pserializer=fetchdataSerializers(result_page,many=True)
# return paginator.get_paginated_response(pserializer.data)
elif request.method=='POST':
serializer=fetchdataSerializers(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data,status=status.HTTP_201_CREATED)
return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 10
}
urls.py
urlpatterns = [
path('', m.index, name='index'),
]
I am even posting the pagination.py file which I was using earlier for PageNumberPagination. Please do suggest me if there is any necessary changes needed to be done or I should remove the file as a whole itself????
pagination.py
from rest_framework import pagination
class StandardResultsSetPagination(pagination.PageNumberPagination):
page_size = 10
page_query_param = 'page'
page_size_query_param = 'per_page'
max_page_size = 1000