From 1b43a0bf65ba0d2e8c7ba75797bb5f5cfa867185 Mon Sep 17 00:00:00 2001 From: Damillora Date: Thu, 17 Dec 2020 00:51:29 +0700 Subject: [PATCH] Add pagination --- songs/templates/songs/index.html | 17 ++++++++++++++--- songs/views.py | 6 +++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/songs/templates/songs/index.html b/songs/templates/songs/index.html index db97d03..0ff0d2c 100644 --- a/songs/templates/songs/index.html +++ b/songs/templates/songs/index.html @@ -4,7 +4,18 @@ {% block content %}

Songs

- + {% include 'components/song-table.html' with songs=page_obj %} + {% endblock %} \ No newline at end of file diff --git a/songs/views.py b/songs/views.py index 0057f69..56b61d9 100644 --- a/songs/views.py +++ b/songs/views.py @@ -1,10 +1,14 @@ +from django.core.paginator import Paginator from django.shortcuts import render, redirect from .models import Song # Create your views here. def song_index(request): songs = Song.objects.all() - return render(request,'songs/index.html',{'songs':songs}) + paginator = Paginator(songs, 100) + page_number = request.GET.get('page',1) + page_obj = paginator.get_page(page_number) + return render(request,'songs/index.html',{'page_obj':page_obj}) def song_id(request, id): song = Song.objects.filter(id=id)[0]