[Answer]-How to implement end less pagination in Django

1👍

Please try out my code:

views.py :

from models import Book
from django.template import RequestContext 
from django.shortcuts import render_to_response

#------------------------------------------------------------------------------ 


def latest_books(request,template = 'latest_books.html',
                  page_template = 'latest_books_page.html' ):

    context = {}    
    book_list = Book.objects.order_by('-pub_date')

    context.update( {'book_list': book_list, 'page_template': page_template,} )

    # override the template and use the 'page' style instead.
    if request.is_ajax():
        template = page_template

    return render_to_response(
        template, context, context_instance=RequestContext(request) )

latest_books.html :

<html><head><title>Books</title></head>

<body>

<h1>Books</h1>
<h2>Viewing All Entries</h2>

<div class="endless_page_template">
    {% include page_template %}
</div>

{% block js %}
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script src="{{ STATIC_URL }}js/endless_on_scroll.js"></script>
<script src="{{ STATIC_URL }}js/endless-pagination.js"></script>    
<script>
    $.endlessPaginate({paginateOnScroll: true,
    endless_on_scroll_margin : 10,
    paginateOnScrollChunkSize: 5        
});</script>
{% endblock %}


</body></html>

latest_books_page.html :

{% load endless %}
{% paginate 10 book_list %}
{% for book in book_list %}
{{ book.name }}<br> {{ book.pub_date }}<br><br><br>
{% endfor %}
{% show_more "even more" "working" %}

Try out and let me know … and enter 20-30 entries into your DB to check it out properly …

0👍

You have to include page_template before call the {{block.super}}

<h2>Entries:</h2>
{% include page_template %}

{% block js %}
    {{ block.super }}
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="{{ STATIC_URL }}endless_pagination/js/endless-pagination.js"></script>
    <script>$.endlessPaginate();</script>
{% endblock %}

Leave a comment