1from django.contrib.auth.models import User
2from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
3
4def index(request):
5 user_list = User.objects.all()
6 page = request.GET.get('page', 1)
7
8 paginator = Paginator(user_list, 10)
9 try:
10 users = paginator.page(page)
11 except PageNotAnInteger:
12 users = paginator.page(1)
13 except EmptyPage:
14 users = paginator.page(paginator.num_pages)
15
16 return render(request, 'core/user_list.html', { 'users': users })
1{% for contact in page_obj %}
2 {# Each "contact" is a Contact model object. #}
3 {{ contact.full_name|upper }}<br>
4 ...
5{% endfor %}
6
7<div class="pagination">
8 <span class="step-links">
9 {% if page_obj.has_previous %}
10 <a href="?page=1">« first</a>
11 <a href="?page={{ page_obj.previous_page_number }}">previous</a>
12 {% endif %}
13
14 <span class="current">
15 Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
16 </span>
17
18 {% if page_obj.has_next %}
19 <a href="?page={{ page_obj.next_page_number }}">next</a>
20 <a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
21 {% endif %}
22 </span>
23</div>
24
1class UserListView(ListView):
2 model = User
3 template_name = 'core/user_list.html' # Default: <app_label>/<model_name>_list.html
4 context_object_name = 'users' # Default: object_list
5 paginate_by = 10
6 queryset = User.objects.all() # Default: Model.objects.all()
1from django.core.paginator import Paginator
2from django.shortcuts import render
3
4from myapp.models import Contact
5
6def listing(request):
7 contact_list = Contact.objects.all()
8 paginator = Paginator(contact_list, 25) # Show 25 contacts per page.
9
10 page_number = request.GET.get('page')
11 page_obj = paginator.get_page(page_number)
12 return render(request, 'list.html', {'page_obj': page_obj})
1# stack overflow url: https://stackoverflow.com/questions/35830779/django-rest-framework-apiview-pagination
2
3from rest_framework.pagination import LimitOffsetPagination
4
5class EventNewsItems(APIView, LimitOffsetPagination):
6
7 def get(self, request, pk, format=None):
8 event = Event.objects.get(pk=pk)
9 news = event.get_news_items().all()
10
11 results = self.paginate_queryset(news, request, view=self)
12 serializer = NewsItemSerializer(results, many=True)
13 return self.get_paginated_response(serializer.data)
14