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):
I did it only on templates with expressions:
{% if is_paginated %}
{% if page_obj.number >= 5 %}
- 1
- ...
{% elif page_obj.number == 4 %}
- 1
{% endif %}
{% if page_obj.number|add:"-2" >= 1 %}
- {{ page_obj.number|add:"-2" }}
{% endif %}
{% if page_obj.number|add:"-1" >= 1 %}
- {{ page_obj.number|add:"-1" }}
{% endif %}
- {{ page_obj.number }}
{% if page_obj.number|add:"1" <= paginator.num_pages %}
- {{ page_obj.number|add:"1" }}
{% endif %}
{% if page_obj.number|add:"2" <= paginator.num_pages %}
- {{ page_obj.number|add:"2" }}
{% endif %}
{% if page_obj.number|add:"2" <= paginator.num_pages|add:"-2" %}
- ...
- {{ paginator.num_pages }}
{% elif page_obj.number|add:"1" <= paginator.num_pages|add:"-2" %}
- {{ paginator.num_pages }}
{% endif %}
{% 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.