DEV Community

Cover image for Pagination in Django
Odipo Otieno
Odipo Otieno

Posted on

Pagination in Django

Django pagination is a technique used to split a large set of data into smaller, more manageable chunks, often displayed across multiple pages. This is particularly useful when you have a large dataset and you want to display it to users in a user-friendly way.

Here's a step-by-step guide on how to implement pagination in Django:

  1. Install Django: If you haven't already, install Django using pip:
   pip install django
Enter fullscreen mode Exit fullscreen mode
  1. Create a Django app (if you haven't already):
   python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode
  1. In your views, use pagination:

Open the view where you want to implement pagination (usually a view that returns a list of objects). Import the necessary modules:

   from django.core.paginator import Paginator
   from django.shortcuts import render
   from .models import YourModel  # Import your model
Enter fullscreen mode Exit fullscreen mode

Then, in your view function:

   def your_view(request):
       all_objects = YourModel.objects.all()
       paginator = Paginator(all_objects, 10)  # Set 10 items per page

       page_number = request.GET.get('page')
       page_obj = paginator.get_page(page_number)

       return render(request, 'template.html', {'page_obj': page_obj})
Enter fullscreen mode Exit fullscreen mode

In this example, YourModel is the model for which you want to paginate. Adjust the queryset and the number of items per page as needed.

  1. Create a template to display paginated data:

Create a template (e.g., template.html) to display the paginated data. In your template, you can use page_obj to access the paginated queryset and the paginator information:

   {% for item in page_obj %}
       <!-- Display item information -->
   {% endfor %}

   <!-- Pagination controls -->
   <div class="pagination">
       <span class="step-links">
           {% if page_obj.has_previous %}
               <a href="?page=1">&laquo; first</a>
               <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>
               <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
           {% endif %}
       </span>
   </div>
Enter fullscreen mode Exit fullscreen mode

This template code provides simple pagination controls and displays the current page number and total number of pages.

  1. Update URLs:

Make sure that your URLs are set up to handle pagination. In your urls.py file, you can add a path for the view you've created.

   from django.urls import path
   from . import views

   urlpatterns = [
       path('your_view/', views.your_view, name='your_view'),
   ]
Enter fullscreen mode Exit fullscreen mode
  1. Run your server:
   python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Now, when you access the corresponding URL (e.g., http://localhost:8000/your_view/), you should see the paginated data.

Remember to replace YourModel with your actual model and customize the template and view as per your specific application. This basic setup can be extended and customized further based on your requirements.

In Django pagination, page_obj is a variable commonly used in templates to represent the current page of paginated data. It's an instance of the Page class from Django's Paginator module.

Here's what page_obj contains:

  1. number: This attribute contains the current page number.

  2. object_list: This attribute contains the list of objects for the current page.

  3. paginator: This attribute contains the Paginator object itself, which has information about the total number of pages, the number of items per page, etc.

  4. has_previous: This is a boolean attribute that indicates whether there is a previous page.

  5. has_next: This is a boolean attribute that indicates whether there is a next page.

  6. previous_page_number: This attribute contains the page number of the previous page.

  7. next_page_number: This attribute contains the page number of the next page.

  8. start_index: This attribute contains the index of the first item on the current page.

  9. end_index: This attribute contains the index of the last item on the current page.

By using page_obj in your template, you can easily access and display information about the current page and the paginated data it contains.

For example, in a Django template, you might loop through page_obj.object_list to display a list of items on the current page, and use page_obj.paginator.num_pages to display the total number of pages.

Here's an example of how you might use page_obj in a Django template:

{% for item in page_obj.object_list %}
    <!-- Display item information -->
{% endfor %}

<!-- Pagination controls -->
<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <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>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>
Enter fullscreen mode Exit fullscreen mode

In this example, page_obj.object_list is used to iterate through the list of objects on the current page, and page_obj.number is used to display the current page number. The pagination controls also use attributes like has_previous, previous_page_number, has_next, and next_page_number to generate links for navigating between pages.

Top comments (0)