Created
January 21, 2022 18:08
-
-
Save juanifioren/2b87c5546f75a13817a8baed967a6311 to your computer and use it in GitHub Desktop.
Django pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="pagination mb-0"> | |
{% if page_obj.has_previous %} | |
<li class="page-item"> | |
<a id="previous-page" class="page-link" href="#" aria-label="Previous">{% trans 'Previous' %}</a> | |
</li> | |
{% else %} | |
<li class="page-item disabled"> | |
<span class="page-link">{% trans 'Previous' %}</span> | |
</li> | |
{% endif %} | |
<li class="page-item disabled"> | |
<span class="page-link">Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</span> | |
</li> | |
{% if page_obj.has_next %} | |
<li class="page-item"> | |
<a id="next-page" class="page-link" href="#" aria-label="Previous">{% trans 'Next' %}</a> | |
</li> | |
<li class="page-item"> | |
<a id="last-page" class="page-link" href="#" aria-label="Previous">{% trans 'Last' %}</a> | |
</li> | |
{% else %} | |
<li class="page-item disabled"> | |
<span class="page-link">{% trans 'Next' %}</span> | |
</li> | |
<li class="page-item disabled"> | |
<span class="page-link">{% trans 'Last' %}</span> | |
</li> | |
{% endif %} | |
</ul> | |
{% if page_obj.has_previous or page_obj.has_next %} | |
<script type="text/javascript"> | |
var urlParams = new URLSearchParams(window.location.search); | |
{% if page_obj.has_previous %} | |
urlParams.delete('page'); | |
urlParams.append('page', '{{ page_obj.previous_page_number }}'); | |
document.getElementById('previous-page').href = '?' + urlParams.toString(); | |
{% endif %} | |
{% if page_obj.has_next %} | |
urlParams.delete('page'); | |
urlParams.append('page', '{{ page_obj.next_page_number }}'); | |
document.getElementById('next-page').href = '?' + urlParams.toString(); | |
urlParams.delete('page'); | |
urlParams.append('page', '{{ page_obj.paginator.num_pages }}'); | |
document.getElementById('last-page').href = '?' + urlParams.toString(); | |
{% endif %} | |
</script> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment