urls.py
urlpatterns = [ // 省略 path('client/<int:num>', views.client, name='client'), // 省略 ]
views.py
from django.core.paginator import Paginator def client(request, num=1): data = Clients.objects.all() page = Paginator(data, 3) params = { 'data' : page.get_page(num) } return render(request, 'sales/client.html', params)
templateにページネーションを実装します。
*.html
<div class="list"> <ul class="pagination justify-content-end"> {% if data.has_previous %} <li class="page-item"> <a class="page-link" href="/client/1">« first</a> </li> <li class="page-item"> <a class="page-link" href="/client/{{data.previous_page_number}}">« prev</a> </li> {% else %} <li class="page-item"> <a class="page-link">« first</a> </li> <li class="page-item"> <a class="page-link">« prev</a> </li> {% endif %} <li class="page-item"> <a class="page-link">{{data.number}}/{{data.paginator.num_pages}}</a> </li> {% if data.has_next %} <li class="page-item"> <a class="page-link" href="/client/{{data.next_page_number}}">next »</a> </li> <li class="page-item"> <a class="page-link" href="/client/{{data.paginator.num_pages}}">last »</a> </li> {% else %} <li class="page-item"> <a class="page-link">next »</a> </li> <li class="page-item"> <a class="page-link">last »</a> </li> {% endif %} </ul> </div>
あとは、client/にリクエストがあった時に、Page not foundとレスポンスを返してしまうので、urls patternでclient/