出什么问题了?我找不到问题...
我该怎么办?
错误:
/
的AttributeError
(“ NoneType”对象没有属性“ page_range”)
异常位置:get_context_data,第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
答案 0 :(得分:1)
应为paginate_by
,而不是paginated_by
class Index(ListView):
model = Post
template_name = 'index.html'
context_object_name = 'object'
paginate_by = 5
... # your code