Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
Created April 10, 2015 10:16
Show Gist options
  • Save MasterAlish/cd237d12d0930a6c82ca to your computer and use it in GitHub Desktop.
Save MasterAlish/cd237d12d0930a6c82ca to your computer and use it in GitHub Desktop.
Pagination python
# total - total pages count
# page - current page
# neighbour_count - nighbour pages count, from each side
#
# 'first' - link to first page
# 'last' - link to last page
def pagination(total, page=1, neighbour_count=2):
result = []
if page-neighbour_count > 2:
result.append('first')
if page-neighbour_count == 2:
result.append(1)
for i in range(page-neighbour_count, page+neighbour_count+1):
if 0 < i <= total:
result.append(i)
if page+neighbour_count < total-1:
result.append('last')
if page+neighbour_count == total-1:
result.append(total)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment