dkoug Blog

Django pagination function

Django -- Posted on Sept. 20, 2018

Django pagination function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


def get_pagination(request, queryset, items):
    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 items_page