Pagination
from django.core.paginator import Paginator, EmptyPage,PageNotAnInteger
from .models import Student
student = Student.objects.all()
paginator = Paginator(student, 2) # Show 25 contacts per page
page = request.GET.get('page')
try:
contacts = paginator.page(page)
except PageNotAnInteger:
contacts = paginator.page(1)
except EmptyPage:
contacts = paginator.page(paginator.num_pages)
return render(request, 'index.html', {'contacts': contacts})
args = {}
args['student'] = student
return render_to_response('index.html',args)
index.html
<!-- head section -->
{% load staticfiles %}
<body>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Student Table</div>
<!-- Table -->
<table class="table">
<tr>
<th style="width:7%;">first name</th>
<th style="width:7%">last name</th>
</tr>
{% for n in contacts %}
<tr>
<td>{{n.firstname}}</td>
<td>{{n.lastname}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
<div style="text-align:center">
<div class="pagination" align='center'>
<span class="step-links">
{% if contacts.has_previous %}
<a href="?page={{ contacts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>
{% if contacts.has_next %}
<a href="?page={{ contacts.next_page_number }}">next</a>
{% endif %}
</span>
</div>
</div>
</body>
</body>