Mux
Mux

Reputation: 45

Pyhon Django what's the problem? / 'NoneType' object has no attribute 'page_range'

what's the problem? i can't find problem...
what should i do?

Error :
AttributeError at /
('NoneType' object has no attribute 'page_range')

Exception Location: get_context_data, line 15

from django.views.generic import ListView
from django.core.paginator import Paginator
from post.models import Post

class Index(ListView):
    model = Post
    template_name = 'index.html'
    context_object_name = 'object'
    paginated_by = 5

    def get_context_data(self, **kwargs):
        context = super(Index, self).get_context_data(**kwargs)
        paginator = context['paginator']
        page_numbers_range = 5
        max_index = len(paginator.page_range) // <--- error line

        page = self.request.GET.get('page')
        current_page = int(page) if page else 1
        start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
        end_index = start_index + page_numbers_range

        if end_index >= max_index:
            end_index = max_index

        page_range = paginator.page_range[start_index:end_index]
        context['page_range'] = page_range
        return context

Upvotes: 1

Views: 442

Answers (1)

JPG
JPG

Reputation: 88659

It should be paginate_by instead of paginated_by

class Index(ListView):
    model = Post
    template_name = 'index.html'
    context_object_name = 'object'
    paginate_by = 5
    ... # your code

Upvotes: 1

Related Questions