Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

11 Pagination

scotwk edited this page May 10, 2015 · 3 revisions

CBVs have some handy functionality built-in, for example pagination.

We are already using the pagination in the NoteList view, but we need to update our template.

Template

Update your list template to include the pagination:

{% if is_paginated %}
<div class="pagination">
   <span class="step-links">
       {% if page_obj.has_previous %}
           <a href="?page={{ page_obj.previous_page_number }}">previous</a>
       {% endif %}

       <span class="current">
           Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
       </span>

       {% if page_obj.has_next %}
          <a href="?page={{ page_obj.next_page_number }}">next</a>
       {% endif %}
  </span>
</div>
{% endif %}

View

Our view currently returns only a user's notes, but it doesn't sort them.

return Note.objects.filter(owner=self.request.user)

Update it to sort the notes by oldest first using order_by.

return Note.objects.filter(owner=self.request.user).order_by('-pub_date')