Add pagination to branches

This commit is contained in:
Damillora 2020-12-17 01:02:21 +07:00
parent 1b43a0bf65
commit e4958148b1
2 changed files with 19 additions and 2 deletions

View File

@ -5,5 +5,18 @@
<h2>Description</h2>
<p>{{ branch.description }}</p>
<h2>Songs</h2>
{% include 'components/song-table.html' with songs=songs %}
{% include 'components/song-table.html' with songs=page_obj %}
<nav aria-label="Pages">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">First</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% endif %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.number }}">{{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</a></li>
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last</a></li>
{% endif %}
</ul>
</nav>
{% endblock %}

View File

@ -1,3 +1,4 @@
from django.core.paginator import Paginator
from django.shortcuts import render
from django.http import HttpResponse
from .models import Branch, Category
@ -20,4 +21,7 @@ def branch_index(request):
def branch_show(request, acronym):
branch = Branch.objects.filter(acronym=acronym)[0]
songs = Song.objects.filter(branch=branch)
return render(request,"branches/show.html",{'branch':branch,'songs':songs})
paginator = Paginator(songs, 100)
page_number = request.GET.get('page',1)
page_obj = paginator.get_page(page_number)
return render(request,"branches/show.html",{'branch':branch,'page_obj':page_obj})