Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Django APIView Pagination

# stack overflow url: https://stackoverflow.com/questions/35830779/django-rest-framework-apiview-pagination

from rest_framework.pagination import LimitOffsetPagination

class EventNewsItems(APIView, LimitOffsetPagination):

    def get(self, request, pk, format=None):
        event = Event.objects.get(pk=pk)
        news = event.get_news_items().all()

        results = self.paginate_queryset(news, request, view=self)
        serializer = NewsItemSerializer(results, many=True)
        return self.get_paginated_response(serializer.data)
Comment

how to add pagination in Django REST framework??

from rest_framework.response import Response
from .serializers import BlogGetSerializer
from rest_framework.views import APIView
from .models import Blog
from rest_framework.pagination import PageNumberPagination

# my blog list
class MyBlogList(APIView, PageNumberPagination):
    # this will output only 5 objects per page
    page_size = 5

    def get(self, request):
        myBlogList = Blog.objects.get()
        # paginate them
        results = self.paginate_queryset(myBlogList, request, view=self)
        serializer = BlogGetSerializer(results, many=True)
        # send paginated data
        return self.get_paginated_response(serializer.data)
        
# API => http://127.0.0.1:8000/blog/details/2/
Comment

django pagination rest framework

from rest_framework.pagination import LimitOffsetPagination

class ProductsPagination(LimitOffsetPagination):
  default_limit = 10
  max_limit = 100

class EventNewsItems(APIView):

    def get(self, request, pk, format=None):
        event = Event.objects.get(pk=pk)
        news = event.get_news_items().all()
        
        pagination_class = ProductsPagination

        results = self.paginate_queryset(news, request, view=self)
        serializer = NewsItemSerializer(results, many=True)
        return self.get_paginated_response(serializer.data)
Comment

django pagination class based views

from django.core.paginator import Paginator
from django.shortcuts import render

from myapp.models import Contact

def listing(request):
    contact_list = Contact.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page.

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'list.html', {'page_obj': page_obj})
Comment

PREVIOUS NEXT
Code Example
Python :: get turtle pos 
Python :: python Modulo 10^9+7 (1000000007) 
Python :: how to make program speak in python 
Python :: raku fib 
Python :: input in one line python 
Python :: do not show figure matplotlib 
Python :: python smtp sendmail 
Python :: turn characters to alpgabetic numper python 
Python :: function for detecting outliers in python 
Python :: pandas plot date histogram 
Python :: find sum numbers in a list in python 
Python :: disbale tkinter textbox 
Python :: python return value from single cell dataframe 
Python :: django reverse function 
Python :: how to use drf pagination directly 
Python :: numpy divide except 
Python :: python array slice 
Python :: Python from...import statement 
Python :: python index of string 
Python :: new column with multiple conditions 
Python :: fibonacci 
Python :: for python 
Python :: prolog avg of list 
Python :: pytorch load pt file 
Python :: datetime.time to seconds 
Python :: how to replace a word in text file using python 
Python :: Display the data types of the DataFrame 
Python :: Program to find GCD or HCF of two numbers python 
Python :: python save to excel 
Python :: matplotlib twinx legend 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =