What? Why?
The default Django elided pagination (pagination with ellipses) varies in width depending on which page node you have selected. When navigating between page 1 and page 2, the default pagination will increase in width by one node and shift the placement of all nodes to the right. The pagination element will reach a fixed size near the middle of the page set and then start to decrease in width when approaching the end of the set.
This problem is most apparent when clicking the "next" button to traverse the pagination element - with each click of the "next" button, the placement of this button moves to the right, causing the user to re-aim the mouse pointer to click again.
Better Elided Pagination extends the Django Pagination class to provide elided pagination that does not vary in width when the selected page is near the start or end of the list. The list of pagination nodes will have a predictable and fixed length when the number of nodes exceeds the desired length of the node list.
The following illustrates the difference between Django's built in elided pagination and this project's "better" elided pagination:
Compatible with Django 3.0+
Compatible with Tailwind 1.9.6+ if using Tailwind CSS
This will install the latest stable release from PyPi.
pip install django-better-elided-pagination
The complete example in the next section assumes you are using Tailwind CSS in your project. The html_list property will generate a list of HTML nodes styled using Tailwind CSS classes. If you prefer to style the pagination nodes yourself, you may simply use:
from better_elided_pagination.paginators import BetterElidedPaginator
def some_view(request):
items = [str(x) for x in range(1, 41)] # the items to be paginated - can be a list or queryset
pagination = BetterElidedPaginator(
request,
items,
3, # items per page
)
elided_list = pagination.get_elided_page_range()
.get_elided_page_range() will return a generator fuction that you can comprehend into a list, or you can loop over to view the nodes:
print([p for p in elided_list])
The generator function will return a list of tuples with the first element representing the integer page number of the node, and the second element represeting the text label of the node.
The content that is being paginated (i.e. the actual list of items to be displayed) can be viewed by using the .item_list property of the BetterElidedPaginator class:
# continued from the example above:
item_list = pagination.item_list
print(item_list)
Import the BetterElidedPaginator to the .py file constructing pagination (views.py) in this example
# views.py
from better_elided_pagination.paginators import BetterElidedPaginator
Then use the class inside a view fuction:
# views.py
def homepage(request):
# the items to be paginated - can be a list or queryset
items = [str(x) for x in range(1, 41)]
pagination = BetterElidedPaginator(
request,
items,
3, # items per page
)
return render(request=request,
template_name="home.html",
context={
"pagination_items": pagination.html_list,
"display_items": pagination.item_list
})
In this example, the pagination is in its own template using Tailwind CSS:
# pagination.html
<div class="flex items-center space-x-1 w-full my-2">
{% for item in pagination_items %}
{{ item }}
{% endfor %}
</div>
css_classes = {
"tw_base": "rounded py-2 px-4 text-center",
"tw_enabled_hover": "hover:bg-blue-100 hover:text-gray-900 hover:shadow",
"tw_enabled_text_color": "text-gray-800",
"tw_disabled": "bg-transparent text-gray-500 cursor-default focus:shadow-none",
"tw_active": "text-white bg-blue-600 shadow-xl",
"outer_div": "",
}