Django pagination function

Django -- Posted on Sept. 20, 2018

Django pagination function

              
                from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def get_pagination(request, queryset, items):
    '''
    items: The number for pagination

    return tuple (total_pages, paginated queryset) 
    '''
    paginator = Paginator(queryset, items)
    page = request.GET.get('page')
    try:
        items_page = paginator.page(page)
    except PageNotAnInteger:
        items_page = paginator.page(1)
    except EmptyPage:
        items_page = paginator.page(paginator.num_pages)
    return (paginator.num_pages, items_page)
                  
   
            

Related Posts