Django Bootstrap Pagination Helper

I Just writing simple script for creating simple pagination with django and bootstrap.
To use these, what you need to do just add the middleware class to your, within you views just pass your queryset to the paginate function, then just include the html to you template.

Here is the usage example:

class MemberView(TemplateView):
    template_name = 'persons/list.html'
    def get(self, request, *args, **kwargs):
        person_list = Person.objects.all()
        members = paginate(request=request, queryset=person_list, limit=30)

        context = self.get_context()
            'members': members
        return super(MemberView, self).render_to_response(context)

# list.html
{% include 'paging.html' with paginator=members %}

NB: I assumed you are using twitter bootstrap on your project.

class PagingMiddleware(object):
"""Middleware to detect current page within page pagination."""
def process_request(self, request):
if 'page' in request.GET:
request.active_page = int(request.GET.get('page'))
except ValueError:
request.active_page = 1
request.active_page = 1
view raw hosted with ❤ by GitHub
<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 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>
view raw paging.html hosted with ❤ by GitHub
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def paginate(request, queryset=None, limit=10):
Helper to transform given queryset to django paginator.
:request -- Django request object.
:queryset -- Model queryset.
:limit -- Limit record to be display.
paginator = Paginator(queryset, limit)
page = request.GET.get('page')
paginated =
except PageNotAnInteger:
paginated =
except EmptyPage:
paginated =
return paginated
view raw hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.