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()