mark
mark

Reputation: 673

Django Bootstrap Pagination not working

I create an application using, django 1.8 and want to implement pagination pages. I use this add-on: https://github.com/jmcclell/django-bootstrap-pagination

I get an error:

AttributeError at /blog/
'NoneType' object has no attribute 'paginator'

In settings.py I have:

 INSTALLED_APPS = (
        'bootstrap_pagination',
    )

and

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
)

This is my template (list of posts):

{% extends "base.html" %}
{% load bootstrap_pagination %}

{% block content %}
    <ul>
        {% for p in post %}
            <hr>
            <li>
                <h3><a href="{% url 'blog:detail' p.id %}">{{ p.title }}</a></h3>
                <p>{{ p.text | truncatewords:50 }}</p>
            </li>
        {% endfor %}
    </ul>

<div class="pagination-row text-right">
    {% bootstrap_paginate page_obj range=4 %}
</div>

{% endblock %}

{% block content_bottom %}{% endblock content_bottom %}

Upvotes: 1

Views: 3287

Answers (1)

LennyLip
LennyLip

Reputation: 1767

You must prepare page_obj in a view (not Class based) and return it with context.

See https://docs.djangoproject.com/en/1.8/topics/pagination/#using-paginator-in-a-view

So, you view must be like that

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

    page = request.GET.get('page')
    try:
        page_obj = paginator.page(page)
    except PageNotAnInteger:
        page_obj  = paginator.page(1)
    except EmptyPage:
        page_obj  = paginator.page(paginator.num_pages)

    return render_to_response('list.html', {"page_obj": page_obj })

And template

{% extends "base.html" %}
{% load bootstrap_pagination %}

{% block content %}
    <ul>
        {% for p in page_obj %}
            <hr>
            <li>
                <h3><a href="{% url 'blog:detail' p.id %}">{{ p.title }}</a></h3>
                <p>{{ p.text | truncatewords:50 }}</p>
            </li>
        {% endfor %}
    </ul>

<div class="pagination-row text-right">
    {% bootstrap_paginate page_obj range=4 %}
</div>

{% endblock %}

{% block content_bottom %}{% endblock content_bottom %}

update

Also, see at MultipleObjectMixin of django Generic Class view

https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-multiple-object/#django.views.generic.list.MultipleObjectMixin.get_context_data

page_obj: An instance of django.core.paginator.Page. If the page is not paginated, this context variable will be None.

ListView is inheritance of MultipleObjectMixin: https://docs.djangoproject.com/es/1.9/ref/class-based-views/generic-display/#listview

So, when we use generic list class view and set paginate_by variable is enough to activate the pagination:

import models
from django.views.generic import ListView

class CarListView(ListView):
    model = models.Car     
    template_name = 'app/car_list.html'  # optional (the default is app_name/modelNameInLowerCase_list.html; )
    context_object_name = "car_list" 
    paginate_by = 10  #and that's it !!

It is enough to start pagination working.

Upvotes: 1

Related Questions