writing Custom pagination html code in python and django

Poster
Posted by Poster under Python category on | Points: 40 | Views : 245
It uses bootstrap v5 to write the html code for paginations

First create a simple filter like below in any python page

@register.simple_tag
def writePageNumbers(pageSize, totalRecords, thisPage):
pagination = '''<ul class="pagination pagination-lg">'''

pageCount = 0
myMod = totalRecords % pageSize
for p in range(0, totalRecords, pageSize):
if (pageCount == thisPage):
pagination += '<li class="page-item active" aria-current="page"><span class="page-link">' + str(pageCount + 1) + '</span></li>'
else:
pagination += '<li class="page-item"><a class="page-link" href="/articles/'+ str(pageCount) + '">' + str(pageCount + 1) + '</a>'

pageCount += 1
pagination += '</ul>'
return pagination

here
first parameter is pageSize,
second parameter is total number of records to paginate
third parameter is the current page

then from the django template, call it like this
                {% autoescape off %}
{% writePageNumbers 2 totalArticles thisPage %}
{% endautoescape %}

Comments or Responses

Login to post response