Skip to content
50 changes: 50 additions & 0 deletions pagination/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ def _get_count(self):
"""
Returns the total number of objects, across all pages.
"""

raise NotImplementedError
count = property(_get_count)

def _get_num_pages(self):
"""
Returns the total number of pages.
"""

raise NotImplementedError
num_pages = property(_get_num_pages)

Expand All @@ -67,10 +69,36 @@ def _get_page_range(self):
Returns a 1-based range of pages for iterating through within
a template for loop.
"""

raise NotImplementedError
page_range = property(_get_page_range)

class FakeInfinitePaginator(InfinitePaginator):
"""
Paginator designed for cases when it's not important to know how many total
pages. This is useful for any object_list that has no count() method or can
be used to improve performance for MySQL by removing counts.

The orphans parameter has been removed for simplicity and there's a link
template string for creating the links to the next and previous pages.
"""

def _get_count(self):
"""
Returns the total number of objects, across all pages.
"""

return 10000
count = property(_get_count)

def _get_num_pages(self):
"""
Returns the total number of pages.
"""

return 100
num_pages = property(_get_num_pages)

class InfinitePage(Page):

def __repr__(self):
Expand All @@ -80,6 +108,7 @@ def has_next(self):
"""
Checks for one more item than last on this page.
"""

try:
next_item = self.paginator.object_list[
self.number * self.paginator.per_page]
Expand Down Expand Up @@ -107,6 +136,27 @@ def previous_link(self):
return self.paginator.link_template % (self.number - 1)
return None


class CachedCountPaginator(Paginator):
"""
Nearly Identical to the core Django Paginator with the exception that it uses queryset cache counts.
Saves big MySQL Count queries. Object_list must be a model queryset.
"""

def _get_count(self):
"Returns the total number of objects, across all pages."
if self._count is None:
try:
self._count = self.object_list.get_cached_count()
except (AttributeError, TypeError):
# AttributeError if object_list has no count() method.
# TypeError if object_list.count() requires arguments
# (i.e. is of type list).
self._count = len(self.object_list)
return self._count
count = property(_get_count)


class FinitePaginator(InfinitePaginator):
"""
Paginator for cases when the list of items is already finite.
Expand Down
2 changes: 1 addition & 1 deletion pagination/templates/pagination/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
<span>...</span>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
Expand Down
16 changes: 16 additions & 0 deletions pagination/templates/pagination/tailless_pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
<span>...</span>
{% endif %}
{% endfor %}
</div>
{% endif %}
22 changes: 22 additions & 0 deletions pagination/templates/pagination/yipit_pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% if is_paginated %}
{% load i18n %}
<div class="pagination">

{% for page in pages %}
{% if forloop.first %}
<!-- This hack removes elipsis before first page element -->
{% else %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
<span>...</span>
{% endif %}
{% endif %}
{% endfor %}

</div>
{% endif %}
Loading