from django import template

register = template.Library()


@register.filter
def four_before_page(page_range, page):
    """Returns 4 or fewer pages before the given (1-based) page number"""
    return page_range[max(page-5, 0):max(page-1, 0)]


@register.filter
def four_after_page(page_range, page):
    """Returns 4 or fewer pages after the given (1-based) page number"""
    return page_range[page:min(page+4, len(page_range))]

from django import template

register = template.Library()


@register.filter
def four_before_page(page_range, page):
    """Returns 4 or fewer pages before the given (1-based) page number"""
    return list(page_range)[max(page-5, 0):max(page-1, 0)]


@register.filter
def four_after_page(page_range, page):
    """Returns 4 or fewer pages after the given (1-based) page number"""
    return list(page_range)[page:min(page+4, len(page_range))]

Support Paginator.page_range returning an iterator or list
Support Paginator.page_range returning an iterator or list

Paginator.page_range will return an iterator in Django 1.9+

  from django import template
  
  register = template.Library()
  
  
  @register.filter
  def four_before_page(page_range, page):
      """Returns 4 or fewer pages before the given (1-based) page number"""
-     return page_range[max(page-5, 0):max(page-1, 0)]
+     return list(page_range)[max(page-5, 0):max(page-1, 0)]
  
  
  @register.filter
  def four_after_page(page_range, page):
      """Returns 4 or fewer pages after the given (1-based) page number"""
-     return page_range[page:min(page+4, len(page_range))]
+     return list(page_range)[page:min(page+4, len(page_range))]
  
Support Paginator.page_range returning an iterator or list
## Code Before:
from django import template

register = template.Library()


@register.filter
def four_before_page(page_range, page):
    """Returns 4 or fewer pages before the given (1-based) page number"""
    return page_range[max(page-5, 0):max(page-1, 0)]


@register.filter
def four_after_page(page_range, page):
    """Returns 4 or fewer pages after the given (1-based) page number"""
    return page_range[page:min(page+4, len(page_range))]

## Instruction:
Support Paginator.page_range returning an iterator or list
## Code After:
from django import template

register = template.Library()


@register.filter
def four_before_page(page_range, page):
    """Returns 4 or fewer pages before the given (1-based) page number"""
    return list(page_range)[max(page-5, 0):max(page-1, 0)]


@register.filter
def four_after_page(page_range, page):
    """Returns 4 or fewer pages after the given (1-based) page number"""
    return list(page_range)[page:min(page+4, len(page_range))]
