Skip to content

Instantly share code, notes, and snippets.

@muradddd
Created October 20, 2020 18:46
Show Gist options
  • Save muradddd/14edda72fae97b3fc5848de345d55159 to your computer and use it in GitHub Desktop.
Save muradddd/14edda72fae97b3fc5848de345d55159 to your computer and use it in GitHub Desktop.
pagination
<div class="row justify-content-center pagination">
<div id="pagination" class="col-12 col-lg-9 justify-content-center pagination mr-auto">
{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="prev-page"
href="?page={{ page_obj.previous_page_number }}{% if request.GET.topic_search %}&topic_search={{ request.GET.topic_search }}{% elif request.GET.conversation_search %}&conversation_search={{ request.GET.conversation_search }}{% endif %}"></a>
{% endif %}
{% for page_number in page_range %}
{% if page_number == page_obj.number %}
<a class="active">{{ page_number }}</a>
{% else %}
<a
href="?page={{ page_number }}{% if request.GET.topic_search %}&topic_search={{ request.GET.topic_search }}{% elif request.GET.conversation_search %}&conversation_search={{ request.GET.conversation_search }}{% endif %}">{{ page_number }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="next-page"
href="?page={{ page_obj.next_page_number }}{% if request.GET.topic_search %}&topic_search={{ request.GET.topic_search }}{% elif request.GET.conversation_search %}&conversation_search={{ request.GET.conversation_search }}{% endif %}"></a>
{% endif %}
{% endif %}
</div>
</div>
# FOR PAGINATION
page = self.request.GET.get('page', 1) if self.request.GET.get('page', 1) != '' else 1
data = self.get_queryset()
if data:
paginator = Paginator(data, self.paginate_by)
results = paginator.page(page)
index = results.number - 1
max_index = len(paginator.page_range)
start_index = index - 5 if index >= 5 else 0
end_index = index + 5 if index <= max_index - 5 else max_index
context['page_range'] = list(paginator.page_range)[start_index:end_index]
context['count'] = self.get_queryset().count()
else:
context['count'] = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment