- page_number = int(request.GET.get('page', '1'))
- entries = paginator.page(page_number).object_list
- page = paginator.page(page_number)
- except ValueError:
- page_number = 1
- entries = paginator.page(page_number).object_list
- page = paginator.page(page_number)
+ per_page = int(per_page)
+ except (TypeError, ValueError):
+ # Then either it wasn't set or it was set to an invalid value
+ return None, objects
+
+ # There also shouldn't be pagination if the list is too short. Try count()
+ # first - good chance it's a queryset, where count is more efficient.
+ try:
+ if objects.count() <= per_page:
+ return None, objects
+ except AttributeError:
+ if len(objects) <= per_page:
+ return None, objects
+
+ paginator = Paginator(objects, per_page)