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 settings.py, within you views just pass your queryset to the paginate function, then just include the html to you template.

Here is the usage example:

# views.py
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()
        context.update({
            '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:
try:
request.active_page = int(request.GET.get('page'))
except ValueError:
request.active_page = 1
else:
request.active_page = 1
view raw middlewares.py 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>
<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>
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.
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
view raw utils.py hosted with ❤ by GitHub

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.