Pierce O'Neill
Pierce O'Neill

Reputation: 383

Paginator object has no attribute 'get_page'

I am trying to use paginator for the first time on my django project. I am having problems getting it to work. When i run it I get the error AttributeError: 'Paginator' object has no attribute 'get_page'. I have played around with it but cannot seem to resolve it. Can somebody help me please? The error seems to be o the line " babysitters = paginator.get_page(page) "

View.py File

from django.shortcuts import render, get_object_or_404, get_list_or_404
from .models import Babysitter, Education, Reference, Work
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from .choices import numbers_choices, minder_choices, county_choices


# Create your views here.
def all_babysitters(request):
    babysitters = Babysitter.objects.all()
    
    paginator = Paginator(babysitters, 3)
  
    page = request.GET.get('page')
    babysitters = paginator.get_page(page)
    
    return render(request, "babysitters.html", {"babysitters": babysitters})

Upvotes: 0

Views: 5798

Answers (3)

Parthiban Soundram
Parthiban Soundram

Reputation: 738

Here is the working code i am used.. referred this blog

In views.py

from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
    user_list = User.objects.all()
    page = request.GET.get('page', 1)

    paginator = Paginator(user_list, 10)
    try:
        users = paginator.page(page)
    except PageNotAnInteger:
        users = paginator.page(1)
    except EmptyPage:
        users = paginator.page(paginator.num_pages)

    return render(request, 'core/user_list.html', { 'users': users })

Then you just handle it to the html template

user_list.html

<table class="table table-bordered">
  <thead>
    <tr>
      <th>Username</th>
      <th>First name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    {% for user in users %}
      <tr>
        <td>{{ user.username }}</td>
        <td>{{ user.first_name }}</td>
        <td>{{ user.email }}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

{% if users.has_other_pages %}
  <ul class="pagination">
    {% if users.has_previous %}
      <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
    {% else %}
      <li class="disabled"><span>&laquo;</span></li>
    {% endif %}
    {% for i in users.paginator.page_range %}
      {% if users.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
      {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    {% if users.has_next %}
      <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
    {% else %}
      <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
  </ul>
{% endif %}

I think it will be very helpful for you..

Upvotes: 2

alexyichu
alexyichu

Reputation: 3642

I have not used Django other than by using Django-Rest-Framework, so I will share what I do, hopefully, you can extrapolate and use for regular Django. So the following is how I construct a paginated response.

  1. Frontend Application sends a page number in the query params in the URL
  2. Based on that page, I paginate the database query
  3. I return the results, along with the pagination information

For example:

from django.core.paginator import Paginator
PAGE_SIZE = 20

def all_babysitters(request):
    p = request.query_params.get("page", 1)
    babysitters = Babysitter.objects.all()
    paginator = Paginator(babysitters, PAGE_SIZE)
    # page = paginator.page(p) # ~ Django 1.11
    # The following line was added, see comments below for why
    page = paginator.get_page(p) # Django 2.0 +
    nextpage = page.next_page_number() if page.has_next() else None
    data = BabySitterSerializer([i for i in page.object_list], many=True)
    pages = {
        "current": p,
        "next": nextpage,
        "total_pages": paginator.num_pages
        "total_results": paginator.count
    }
    return {
         "page_data": pages
         "data": data
    }

One main difference is the use of a serializer, which I have no idea if you need or don't need for regular Django, but with Django-Rest-Framework you can return the result of a query back to the frontend application in JSON format.

Hope that helps :D

Upvotes: 1

Todor
Todor

Reputation: 16050

The .get_page method was added in Django 2.0, so I guess you are using something older, maybe Django 1.11. For this version use paginator.page(page) instead, but be careful that this may raise an InvalidPage Exception when invalid/no page has been found.

Upvotes: 4

Related Questions