old_contents: from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
  
  DEFAULT_PAGE_KEY = 'p'
  
  
  def paginate(request, items, page_key=DEFAULT_PAGE_KEY, per_page=20):
      page = request.GET.get(page_key, 1)
  
      paginator = Paginator(items, per_page)
      try:
          page = paginator.page(page)
      except PageNotAnInteger:
          page = paginator.page(1)
      except EmptyPage:
          page = paginator.page(paginator.num_pages)
  
      return paginator, page
  
new_contents: from django.core.paginator import Paginator
  
  DEFAULT_PAGE_KEY = 'p'
  
  
  def paginate(request, items, page_key=DEFAULT_PAGE_KEY, per_page=20):
      paginator = Paginator(items, per_page)
      page = paginator.get_page(request.GET.get(page_key))
      return paginator, page
  
subject: Simplify wagtail.utils.pagination using Paginator.get_page