pagination django functional code example
Example 1: django paginator code
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
user_list = User.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(user_list, 10)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
return render(request, 'core/user_list.html', { 'users': users })
Example 2: django paginator code
class UserListView(ListView):
model = User
template_name = 'core/user_list.html'
context_object_name = 'users'
paginate_by = 10
queryset = User.objects.all()