Search code examples
djangodjango-endless-pagination

Losing order of sorted queryset With Django-Endless-Pagination


I've got a few sorted querysets im passing to a template. Multiple paginations are shown per page. The problem is after the first series of paginated items, subsequent ones lose their sort. Here's my code:

views.py

    def entry_index(request, parent_cat, child_cat, template='entry_index.html',  
                    page_template='entry_index_page.html'):  
                context = { 'items_by_percentage_saved':  
                     Item.objects.filter(category=category).order_by('-percentage_saved'),  }
                if request.is_ajax():
                    template = page_template
                    return render_to_response(template, context,  
                                              context_instance=RequestContext(request))

by_percentage_saved.html

{% load endless %}
{% paginate items_by_percentage_saved %}
{% for item in items_by_percentage_saved %}
<div class="large-4 small-6 columns">
  <a class="th" href=""><img style="height: 12em;" src={{ item.image_url }}></a>

  <div class="panel">
    <h5>{{ item.title|truncatechars:50 }}</h5>
    ...
  </div>
</div>
{% endfor %}
{% show_more %}

Update I've done some more debugging and the items_by_percentage_saved queryset is definitely sorted in entry_index(). I put a few breakpoints in by_percentage_saved.html to see if I could figure out whats going on, but strangely after you click "more" to get the next paginated data, entry_index() is called again but the breakpoints never fire a second time in by_percentage_saved.html, even though the new paginated data is generated. Talk about confusing


Solution

  • Ok have solved it by going through the docs again and using page decorators. The docs kinda make it sound like these are optional but my guess they are a requirement for multiple pagination on the same page.

    from endless_pagination.decorators import page_template @page_template('entry_index_page.html')
    @page_template('by_percentage_saved.html', key='by_percentage_saved')
    def entry_index(request, parent_cat, child_cat, template='entry_index.html',
    extra_context=None):