Pagination - 30 days of Django

Pagination - 30 days of Django

/ #Django

Learn how to add pagination so you can split your items into multiple pages.

Django has built in support and functionality for paginating data. So this will be utilized in this Django project.

Let's open up the task/ file, and begin there with these changes:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # New, 1

def frontpage(request):
    if request.method == 'POST':
        form = TaskForm(request.POST)

        if form.is_valid():
        form = TaskForm()

    title = 'This is a variable'
    tasks = Task.objects.filter(user=request.user)
    categories = Category.objects.all()

    page = request.GET.get('page', 1)
    paginator = Paginator(tasks, 10)
        tasks =
    except PageNotAnInteger:
        tasks =
    except EmptyPage:
        tasks =

    return render(request, 'task/frontpage.html', {'title': title, 'tasks': tasks, 'categories': categories, 'form': form})

As you can see here, there are a few new lines. First, we import three methods from the paginator class.

Then, we just get all of the tasks like we've done earlier.
Next step is to get the page number from the URL, if it's not there, we default it to 1.

Next, we set up a Paginator, pass in the tasks and the limit per page (10). Then we check if there is an actual page, if the page is empty and simlar.

So when that's done, we can go to the template and implement it there. Open up frontpage.html:

{% extends 'task/base.html' %}

{% block content %}
    <div class="frontpage">
        <h1>{{ title }}</h1>

        <div class="columns">
            <div class="column is-8">


                {% for task in tasks %}
                        <p>{{ task.title }} - {{ task.created_at }}</p>
                {% endfor %}

                {% if tasks.has_other_pages %}
                    <nav class="pagination" role="navigation" aria-label="pagination">
                        {% if posts.has_previous %}
                            <a class="pagination-previous" href="?page={{ tasks.previous_page_number }}">Previous</a>
                        {% endif %}

                        {% if tasks.has_next %}
                            <a class="pagination-next" href="?page={{ tasks.next_page_number }}">Next</a>
                        {% endif %}

                        <ul class="pagination-list">
                            {% for i in tasks.paginator.page_range %}
                                {% if tasks.number == i %}
                                        <a class="pagination-link is-current">{{ i }}</a>
                                {% else %}
                                        <a class="pagination-link" href="?page={{ i }}">{{ i }}</a>
                                {% endif %}
                            {% endfor %}
                {% endif %}

{% endblock %}

The code for this is more or less self explanatory. We use the tasks object to check if there are pages, if there are any previous pages and similar.

Table of contents


No comments yet...

Add comment


Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.