Django template tag for pagination
from django import template
register = template.Library()
@register.simple_tag
def pagination_links(current_page, total_pages, num_links=4):
start = max(current_page - num_links // 2, 1)
end = min(start + num_links - 1, total_pages)
if end - start < num_links:
start = max(end - num_links + 1, 1)
return range(start, end + 1)