def get(self, page): pager = Paginator(self.db) if int(page) > pager.pages: pager.page = pager.pages elif int(page) < 1: pager.page = 1 else: pager.page = int(page) com = pager.get_company() self.render( "home.html", companies=com, paginator=pager)
def test_last_page(self): paginator = Paginator(Article.objects.all(), 5) p = paginator.page(2) self.assertEqual("<Page 2 of 2>", force_text(p)) self.assertQuerysetEqual(p.object_list, [ "<Article: Article 6>", "<Article: Article 7>", "<Article: Article 8>", "<Article: Article 9>" ], ordered=False) self.assertFalse(p.has_next()) self.assertTrue(p.has_previous()) self.assertTrue(p.has_other_pages()) self.assertEqual(3, p.next_page_number()) self.assertEqual(1, p.previous_page_number()) self.assertEqual(6, p.start_index()) self.assertEqual(9, p.end_index())
def test_paginate_list(self): # Paginators work with regular lists/tuples, too -- not just with QuerySets. paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5) self.assertEqual(9, paginator.count) self.assertEqual(2, paginator.num_pages) self.assertEqual([1, 2], list(paginator.page_range)) p = paginator.page(1) self.assertEqual("<Page 1 of 2>", force_text(p)) self.assertEqual([1, 2, 3, 4, 5], p.object_list) self.assertTrue(p.has_next()) self.assertFalse(p.has_previous()) self.assertTrue(p.has_other_pages()) self.assertEqual(2, p.next_page_number()) self.assertEqual(0, p.previous_page_number()) self.assertEqual(1, p.start_index()) self.assertEqual(5, p.end_index())
def test_first_page(self): paginator = Paginator(Article.objects.all(), 5) p = paginator.page(1) self.assertEqual("<Page 1 of 2>", force_text(p)) self.assertQuerysetEqual(p.object_list, [ "<Article: Article 1>", "<Article: Article 2>", "<Article: Article 3>", "<Article: Article 4>", "<Article: Article 5>" ], ordered=False) self.assertTrue(p.has_next()) self.assertFalse(p.has_previous()) self.assertTrue(p.has_other_pages()) self.assertEqual(2, p.next_page_number()) self.assertEqual(0, p.previous_page_number()) self.assertEqual(1, p.start_index()) self.assertEqual(5, p.end_index())
def test_first_page_instead_of_invalid(self): pagination_module.SHOW_FIRST_PAGE_WHEN_INVALID = True paginator = Paginator(Article.objects.all(), 5) p = paginator.page(7) self.assertEqual("<Page 1 of 2>", force_text(p)) pagination_module.SHOW_FIRST_PAGE_WHEN_INVALID = False