Skip to content

Instantly share code, notes, and snippets.

@Keda87
Last active March 31, 2016 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keda87/1b90e81e840a40bace2041c1a926888f to your computer and use it in GitHub Desktop.
Save Keda87/1b90e81e840a40bace2041c1a926888f to your computer and use it in GitHub Desktop.
Bootstrap Django Pagination
class PagingMiddleware(object):
"""Middleware to detect current page within page pagination."""
def process_request(self, request):
if 'page' in request.GET:
try:
request.active_page = int(request.GET.get('page'))
except ValueError:
request.active_page = 1
else:
request.active_page = 1
<div class="row">
<div class="col-md-5 col-sm-5 lh-xl">
Showing {{ paginator.start_index }} to {{ paginator.end_index }} of {{ paginator.paginator.count }} records.
</div>
<div class="col-md-7 col-sm-7 pull-right">
<ul class="pagination">
<li class="{{ paginator.has_previous|yesno:',disabled' }}"><a href="?page=1"><i class="fa fa-angle-double-left"></i></a></li>
<li class="{{ paginator.has_previous|yesno:',disabled' }}"><a href="{% if paginator.has_previous %}?page={{ paginator.previous_page_number }}{% else %}#{% endif %}"><i class="fa fa-angle-left"></i></a></li>
{% for page in paginator.paginator.page_range %}
<li class="{% if request.active_page == page %}active{% endif %}"><a href="?page={{ page }}">{{ page }}</a></li>
{% endfor %}
<li class="{{ paginator.has_next|yesno:',disabled' }}"><a href="{% if paginator.has_next %}?page={{ paginator.next_page_number }}{% else %}#{% endif %}"><i class="fa fa-angle-right"></i></a></li>
<li class="{{ paginator.has_next|yesno:',disabled' }}"><a href="?page={{ paginator.paginator.num_pages }}"><i class="fa fa-angle-double-right"></i></a></li>
</ul>
</div>
</div>
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def paginate(request, queryset=None, limit=10):
"""
Helper to transform given queryset to django paginator.
Params:
:request -- Django request object.
:queryset -- Model queryset.
:limit -- Limit record to be display.
"""
paginator = Paginator(queryset, limit)
page = request.GET.get('page')
try:
paginated = paginator.page(page)
except PageNotAnInteger:
paginated = paginator.page(1)
except EmptyPage:
paginated = paginator.page(paginator.num_pages)
return paginated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment