def do_pagination(request, all_items): """ :param request: contains the current page requested by user :param all_items: :return: dict containing paginated items and pagination bar """ results_per_page = 20 results = all_items try: page_no = int(request.GET.get('page', 1)) except (TypeError, ValueError): raise Http404("Not a valid number for page.") if page_no < 1: raise Http404("Pages should be 1 or greater.") paginator = Paginator(results, results_per_page) try: page = paginator.page(page_no) except InvalidPage: raise Http404("No such page!") return paginator, page
def test_paginator_with_more_than_10_pages_should_hide_some_of_them(self): results = list(range(0, 300)) results_per_page = 20 paginator = Paginator(results, results_per_page) paginator.page(1) self.assertEqual(paginator.paginate_sections(), [1, 2, 3, 4, 5, None, 11, 12, 13, 14, 15])
def test_paginator_with_less_than_10_pages_should_return_all_pages(self): results = list(range(0, 199)) results_per_page = 20 paginator = Paginator(results, results_per_page) paginator.page(1) self.assertEqual(paginator.paginate_sections(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])