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

{% if page_obj.has_previous %}
  <a href="?page={{ page_obj.previous_page_number }}">« Previous page</a>

  {% if page_obj.number > 3 %}
    <a href="?page=1">1</a>
    {% if page_obj.number > 4 %}
      <span>...</span>
    {% endif %}
  {% endif %}
{% endif %}

{% for num in page_obj.paginator.page_range %}
  {% if page_obj.number == num %}
    <a href="?page={{ num }}">{{ num }}</a>
  {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
    <a href="?page={{ num }}">{{ num }}</a>
  {% endif %}
{% endfor %}

{% if page_obj.has_next %}
  {% if page_obj.number < page_obj.paginator.num_pages|add:'-3' %}
    <span>...</span>
    <a href="?page={{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a>
  {% elif page_obj.number < page_obj.paginator.num_pages|add:'-2' %}
    <a href="?page={{ page_obj.paginator.num_pages }}">{{ page_obj.paginator.num_pages }}</a>
  {% endif %}

  <a href="?page={{ page_obj.next_page_number }}">Next Page »</a>
{% endif %}
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

PREVIOUS NEXT
Code Example
Python :: no python application found, check your startup logs for errors 
Python :: Converting Dataframe from the multi-dimensional list with column name 
Python :: read image file python 
Python :: python list of whole numbers 
Python :: python single line if 
Python :: rename files with spaces in a folder python 
Python :: numpy generate sequence 
Python :: r named chr to dataframe 
Python :: classification cross validation 
Python :: how to return a value from a function in python 
Python :: pytorch calculate mse mae 
Python :: how do a plot on matplotlib python 
Python :: python key from values 
Python :: change password django 
Python :: django rest framework viewset perform_update 
Python :: how to change the disabled color in tkinter 
Python :: logarithmic scale fitting python 
Python :: python crash course 
Python :: calculate pointbiseral correlation 
Python :: get current url with parameters url django 
Python :: Python How to get the keys in a dictionary? 
Python :: cv2 check if image is grayscale 
Python :: reshape array numpy 
Python :: rename column by indexing 
Python :: K-Means Clustering in Python – 3 clusters 
Python :: how to get mac address in python 
Python :: how take in put as list in integer value 
Python :: django rest framework function based views 
Python :: how to center a string python 
Python :: format date string python 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =