I am trying to use Django Endless Pagination to achieve the effect pf a Twitter/Instagram style pagination wherein once the user scrolls to the bottom of the page, then new contents are loaded onto the page. Using the above module, i have been able to do so. However, i have a performance related question. The view from where i send the data still loads all the rows from the database. Only in the template side, a portion of it is displayed.
class IndexView(BaseAjaxViewList):
template_name = 'polls/index.html'
page_template = 'polls/item_index.html'
context_object_name = 'data'
model = Question
def get_context_data(self,**kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
print(len(context["data"]))
return context
I thought that only 'n' number of items would be accessed from database and when scroll happen there would be another db call and so on, which can improve the loading performance of the page. Suppose there are 1000 items in the page, then ideally i would want,say 50, items to load first (50 items to be accessed in 1 db call) and then once a user scrolls and comes to the bottom of the page then next 50 items should be accessed and displayed. However, i tried to check underneath the scene, and it seems all the 1000 calls are made at once using this module. Only at the template side, 50 are shown at a time. Is this true? Is there any django module which does that or i would have to write my own ajax calls etc ?
The templates for reference are below:
The Main template
<h2>Polls:</h2>
{% include page_template %}
{% block js %}
{{ block.super }}
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="{% static 'endless_pagination/js/endless-pagination.js' %}"></script>
<script>$.endlessPaginate();</script>
{% endblock %}
The page_template
:
{% load endless %}
{% paginate entries %}
{% for entry in entries %}
{# your code to show the entry #}
{% endfor %}
{% show_more %}
Django Ajax would allow you to make Ajax calls to a Django view. You could wrap your results in a list then cache that. From there just pop the first 50 (or whatever size you want to set) when you initially render the view. Then pop your cache on the Ajax call.
Something like:
@django_ajax
def paginated_cache(request):
more_results = [cache.get('your_results').pop() for result in range(50)]
# turn this to json, wrap in dictionary and return
A more detailed instruction is on the django ajax page. This is for a smaller result size. For bigger results you may want to use something like a cursor though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With