Wednesday 12 November 2014

Django Endless Pagination – An effective way to implement on scroll pagination

My search for adding a pagination for the cinepeople project ended with Django Endless Pagination. Initially I was looking for a pagination app which is quite normal having previous and next buttons. I started implementing the Django Endless Pagination on the same way but realized that it is very good and effective for on scroll adding content. And it is very easy to implement with few steps below:

Views.py
def my_view(request):
    context = {
        'polls': Poll.objects.all()
    }
    if request.is_ajax():
        return render_to_response(
            'poll_ajax.html', context, context_instance=RequestContext(request))
    return render_to_response(
        'poll.html', context, context_instance=RequestContext(request))

    Poll.html
  <div id="poll">
    {% include 'poll_ajax.html' %}
</div>
 
{% block extra_js %}
    <script src="{{ STATIC_URL }}endless_pagination/js/endless-pagination.js">
</script>
    <script>$.endlessPaginate({paginateOnScroll: true});></script>
{% endblock %}
 
poll_ajax.html
{% load endless %}
{% paginate polls %}
{% for poll in polls %}
    {# your code for poll information #}
{% endfor %}
{% show_more %}