Last active
December 18, 2015 22:49
-
-
Save ErDmKo/5857145 to your computer and use it in GitHub Desktop.
Pages for django queryset's.
This file contains 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
{% if pages|length > 1 %} | |
<ul class="page_list"> | |
{% if sel_page > first_rand %} | |
<li><a href="{{ object.get_absolute_url }}{{ get_str }}&page=1">1</a><span>...</span></li> | |
{% endif %} | |
{% for page in pages %} | |
<li><a href="{{ object.get_absolute_url }}{{ get_str }}&page={{ page }}"{% if page == sel_page %} class="sel"{% endif %}>{{ page }}</a></li> | |
{% endfor %} | |
{% if sel_page <= last_rand %} | |
<li><span>...</span> <a href="{{ object.get_absolute_url }}{{ get_str }}&page={{ last }}">{{ last }}</a></li> | |
{% endif %} | |
</ul> | |
{% endif %} |
This file contains 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
on_page = 2 | |
prod_list = Product.objects | |
prod_count = prod_list.count() | |
page = kwargs.get('page_id', '1') | |
page = page.isdigit() and int(page) or 1 | |
max_pages = int(math.ceil(float(prod_count)/on_page)) | |
first_rand = 5 | |
c.update({ | |
'get_str': '?'+'&'.join(["%s=%s" % (key, val) for key, val in self.request.GET.iteritems()]), | |
'last': max_pages, | |
'first_rand': first_rand, | |
'last_rand': max_pages - first_rand, | |
'pages': range(max(1,page - first_rand),min(max_pages+1,page+first_rand)), | |
'sel_page':page, | |
'object_list': prod_list.all()[(page-1)*on_page:page*on_page] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment