from django.core.paginator import Paginator items = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig'] paginator = Paginator(items, 3) # Get the first page of items page1 = paginator.get_page(1) # Get a shortened page range with an ellipsis pages = paginator.get_elided_page_range(page1.number, on_each_side=1, on_ends=1) # Output the page range print(pages)
from django.core.paginator import Paginator from django.shortcuts import render def my_view(request): items = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig'] paginator = Paginator(items, 3) page = paginator.get_page(request.GET.get('page')) # Get a shortened page range with an ellipsis pages = paginator.get_elided_page_range(page.number, on_each_side=1, on_ends=1) context = { 'page': page, 'pages': pages, } return render(request, 'my_template.html', context)This code defines a view that creates a Paginator object with a list of six items and a page size of three, then gets the current page number from the request's GET parameters (or defaults to 1). It then calls the get_elided_page_range method to get a shortened page range with an ellipsis, passing in the current page number and other parameters as before. Finally, it creates a context dictionary with the current page object and the page range, and renders a template with that context.