Django template tag for pagination

Django July 02, 2024 python

Django template tag for pagination

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