Created
April 10, 2015 10:16
-
-
Save MasterAlish/cd237d12d0930a6c82ca to your computer and use it in GitHub Desktop.
Pagination python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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