Search code examples
pythondjangodjango-viewsdjango-templatesdjango-pagination

Django pagination has_previous and has_next methods doesn't work


I'm using pagination in Django 1.11. But when I use has_next and has_previous it returns false everytime. Don't print anything. I'm sure that there is previous and next pages. In views I'm using TableView which is like ListView. Here is my code:

<div class="pagination">
        <span class="step-links">

          {% if customers.has_next %}
            <p>HAS NEXT</p>
          {% endif %}
          {% if customers.has_previous %}
            <p>HAS PREVİOUS</p>
          {% endif %}




        </span>
      </div>

views.py

class TableView(ExportMixin, SingleTableView):
    model = Customer
    table_class = CustomerTable
    template_name = 'admin_pages/user_admin_page.html'
    context_object_name = 'customers'
    paginate_by = 3

*I'm using tableview just for export table.


Solution

  • You should work with the page_obj object, so:

    {% if page_obj.has_next %}
      <p>HAS NEXT</p>
    {% endif %}
    {% if page_obj.has_previous %}
      <p>HAS PREVİOUS</p>
    {% endif %}

    Note: is no longer supported [Django-doc] since April 2020, you should consider upgrading the Django version.