“Pagination django apiview” Réponses codées

Vues basées sur la classe de pagination de Django

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})
Repulsive Rat

Pagination django apiview

# 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)
Bored Bug

Réponses similaires à “Pagination django apiview”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code