I am using the django paginator in the template. Its working ok, but not good when there\'s large numbers of pages.
views.py:
def blog(request):
Gonna throw this in. I came up with it because it lets you know there are more pages on either side.
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}"><i class="fa fa-chevron-left" aria-hidden="true"></i></a></li>
{% else %}
<li class="disabled"><span><i class="fa fa-chevron-left" aria-hidden="true"></i></span></li>
{% endif %}
{% if page_obj.number|add:'-4' > 1 %}
<li><a href="?page={{ page_obj.number|add:'-5' }}">…</a></li>
{% endif %}
{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.paginator.num_pages > page_obj.number|add:'4' %}
<li><a href="?page={{ page_obj.number|add:'5' }}">…</a></li>
{% endif %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}"><i class="fa fa-chevron-right" aria-hidden="true"></i></a></li>
{% else %}
<li class="disabled"><span><i class="fa fa-chevron-right" aria-hidden="true"></i></span></li>
{% endif %}
</ul>
And it looks like this:
I did it only on templates with expressions:
{% if is_paginated %}
<div class="text-center">
<ul class="pagination pagination-sm">
{% if page_obj.number >= 5 %}
<li><a href="?page=1">1</a></li>
<li><span>...</span></li>
{% elif page_obj.number == 4 %}
<li><a href="?page=1">1</a></li>
{% endif %}
{% if page_obj.number|add:"-2" >= 1 %}
<li><a href="?page={{ page_obj.number|add:"-2" }}">{{ page_obj.number|add:"-2" }}</a></li>
{% endif %}
{% if page_obj.number|add:"-1" >= 1 %}
<li><a href="?page={{ page_obj.number|add:"-1" }}">{{ page_obj.number|add:"-1" }}</a></li>
{% endif %}
<li class="active"><a href="?page={{ page_obj.number }}">{{ page_obj.number }}</a></li>
{% if page_obj.number|add:"1" <= paginator.num_pages %}
<li><a href="?page={{ page_obj.number|add:"1" }}">{{ page_obj.number|add:"1" }}</a></li>
{% endif %}
{% if page_obj.number|add:"2" <= paginator.num_pages %}
<li><a href="?page={{ page_obj.number|add:"2" }}">{{ page_obj.number|add:"2" }}</a></li>
{% endif %}
{% if page_obj.number|add:"2" <= paginator.num_pages|add:"-2" %}
<li><span>...</span></li>
<li><a href="?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% elif page_obj.number|add:"1" <= paginator.num_pages|add:"-2" %}
<li><a href="?page={{ paginator.num_pages }}">{{ paginator.num_pages }}</a></li>
{% endif %}
</ul>
</div>
{% endif %}
I know django is like "dont write your code again" but i found this easier for me to understand right now. Hope i helped.