How organize pagination with a large number of pages in Django project?
I change pagination.html. Please try this.
<nav aria-label="pagination" class="pagination_area">
<div class="row">
{% if page.end_index > 0 %}
<div class="col-sm-12 col-md-5 d-none d-md-block">
<p>Showing {{ page.start_index }} to {{ page.end_index }} of {{ page.paginator.count}}.</p>
</div>
{% endif %}
{% if page.paginator.num_pages > 1 %}
<div class="col-sm-12 col-md-7 dataTables_pager">
<ul class="pagination">
{% if page.has_previous %}
<li class="page-item">
<a class="page-link" data-page="1" href="?page={{ page.previous_page_number }}">
<i class="fa fa-angle-double-left"></i>
</a>
</li>
{% if page.previous_page_number > 1 %}
<li class="page-item">
<a class="page-link " data-page="{{page.previous_page_number}}" href="?page={{ page.previous_page_number }}">
<i class="fa fa-angle-left"></i>
</a>
</li>
{% endif %}
{% endif %}
{% if page.previous_page_number > 2 %}
<li class="page-item">
<a class="page-link " data-page="{{page.number|add:'-2'}}" href="?{{page.number|add:'-2'}}"> {{ page.number|add:"-2" }} </a>
</li>
<li class="page-item">
<a class="page-link " data-page="{{page.number|add:'-1'}}" href="?page={{page.number|add:'-1'}}"> {{ page.number|add:"-1" }} </a>
</li>
{% endif %}
<li class="page-item active"><span class="page-link ">{{ page.number }}</span></li>
{% if page.paginator.num_pages > page.number|add:"2" %}
<li class="page-item">
<a class="page-link " data-page="{{page.number|add:'1'}}" href="?page={{page.number|add:'1'}}"> {{ page.number|add:"1" }} </a>
</li>
<li class="page-item">
<a class="page-link " data-page="{{page.number|add:'2'}}" href="?page={{page.number|add:'2'}}"> {{ page.number|add:"2" }} </a>
</li>
{% endif %}
{% if page.has_next %}
<li class="page-item">
<a class="page-link " data-page="{{page.next_page_number}}" href="?page={{ page.next_page_number }}">
<i class="fa fa-angle-right"></i>
</a>
</li>
<li class="page-item">
<a class="page-link " data-page="{{page.paginator.num_pages}}" href="?page={{page.paginator.num_pages}}">
<i class="fa fa-angle-double-right"></i>
</a>
</li>
{% endif %}
</ul>
</div>
{% endif %}
</div>
</nav>
My design just like this. If you change design for your needs.
Here click <<
to go first page , >>
to go last page , <
to go previous page and >
to go next page.
Django how to sort and paginate nested ORM and non-ORM object
I Found a simple solution:
class MyViewSet(viewsets.ModelViewSet):
queryset = MyModel.Property.objects.filter(status)\
.prefetch_related('owner__user')\
.prefetch_related('host__user')\
.prefetch_related('category__user')
serializer_class = MySerializer
filterset_class = MyFilterSet
filter_backends = [filters.OrderingFilter, dj_filters.DjangoFilterBackend]
def get_queryset(self):
today = str(datetime.now())
start_date = self.request.query_params.get('start_date')
end_date = self.request.query_params.get('end_date')
'''
validate start_date and end_date
'''
return self.queryset
def list(self, request, *args, **kwargs):
response = super().list(request, args, kwargs)
response.request = self.request
ordering = request.query_params.get('ordering')
# ORDERING WORKING
if ordering:
response.data = sorted(
response.data,
key=operator.itemgetter(ordering.replace('-', ''),)
)
if "-" in ordering:
response.data = sorted(
response.data,
key=lambda k: (k[ordering.replace('-', '')], ),
reverse=True
)
else:
response.data = sorted(
response.data,
key=lambda k: (k[ordering], )
)
# HERE IS ALL I NEED TO DO TO GET PAGINATION WORK
paginator = StandardResultsSetPagination()
result_page = paginator.paginate_queryset(response.data, request)
return paginator.get_paginated_response(result_page)
Paginate Django without changing url
Please try this. Here we using get_page
method.
...
claim_documents = ClaimDocument.objects.all().order_by('-created_date')
paginator = Paginator(claim_documents, 2)
page = request.GET.get('page')
paginator = Paginator(data, 10)
page_obj = paginator.get_page(page)
context = {
'page_obj': page_obj,
...
}
This link may help you.
Related Topics
CSS: Display:Block; Vs Display:Table;
How to Have Attributes on Closing Tags
Stop Lastpass Filling Out a Form
Always Show Browser Scrollbar to Prevent Page Jumping
Favicon Standard - 2022 - Svg, Ico, Png and Dimensions
Internet Explorer 8 Ignores Width for 'Display: Table-Cell' Element
Border Around Tr Element Doesn't Show
Complete List of Reasons Why a CSS File Might Not Be Working
Accessibility: Recommended Alt-Text Convention for Svg and Mathml
In Chrome 55, Prevent Showing Download Button for HTML 5 Video
Display Text on Mouseover for Image in HTML
CSS Position Element "Fixed" Inside Scrolling Container