Django:Paginator错误:类型为';用户';没有len()
我正在学习使用分页器,因为我的列表越来越长了!我在实现它时遇到了问题,因为我遇到了一个错误。我注意到,如果我删除Django:Paginator错误:类型为';用户';没有len(),django,django-class-based-views,paginator,Django,Django Class Based Views,Paginator,我正在学习使用分页器,因为我的列表越来越长了!我在实现它时遇到了问题,因为我遇到了一个错误。我注意到,如果我删除paginate_by=10,错误就会消失,但我非常确定这是分页所必需的。为什么get_queryset与paginator冲突 class NotificationsListView(ListView): template_name = "notices/list.html" paginate_by = 10 def get_context_data(sel
paginate_by=10
,错误就会消失,但我非常确定这是分页所必需的。为什么get_queryset与paginator冲突
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_context_data(self, *args, **kwargs):
context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
qs = self.get_queryset().notifications.all()
paginator = Paginator(qs , self.paginate_by)
page = self.request.GET.get('page')
try:
notification_pages = paginator.page(page)
except PageNotAnInteger:
notification_pages = paginator.page(1)
except EmptyPage:
notification_pages = paginator.page(paginator.num_pages)
context['notifications'] = notification_pages
return context
def get_queryset(self, *args, **kwargs):
request = self.request
return User.objects.get(pk=self.request.user.pk)
回溯:
File "myapp\lib\site-packages\django\core\paginator.py" in count
85. return self.object_list.count()
During handling of the above exception ('User' object has no attribute 'count'), another exception occurred:
File "myapp\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\base.py" in dispatch
89. return handler(request, *args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get
157. context = self.get_context_data()
File "myapp\src\notices\views.py" in get_context_data
18. context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
File "myapp\lib\site-packages\django\views\generic\list.py" in get_context_data
119. paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
File "myapp\lib\site-packages\django\views\generic\list.py" in paginate_queryset
69. page = paginator.page(page_number)
File "myapp\lib\site-packages\django\core\paginator.py" in page
65. number = self.validate_number(number)
File "myapp\lib\site-packages\django\core\paginator.py" in validate_number
43. if number > self.num_pages:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in num_pages
95. if self.count == 0 and not self.allow_empty_first_page:
File "myapp\lib\site-packages\django\utils\functional.py" in __get__
36. res = instance.__dict__[self.name] = self.func(instance)
File "myapp\lib\site-packages\django\core\paginator.py" in count
90. return len(self.object_list)
Exception Type: TypeError at /notices/
Exception Value: object of type 'User' has no len()
get\u queryset
应返回queryset
而不是对象
还有另一个方法叫做get\u object
,它返回一个对象。get\u queryset
应该返回一个queryset
而不是对象
还有另一个方法叫做get\u object
,它返回一个对象。您想显示特定用户的通知列表,对吗?然后,您应该从get\u queryset
方法返回通知列表
我认为您应该这样做,在我看来,您不应该自己处理分页,django/django restframework应该在内部处理它。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
return self.request.user.notifications.all()
使用Django Rest框架,您只需要在设置文件中添加默认分页器,类似于这样
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}
或者,如果要使用现有代码,可以尝试以下方法:
def listing(request):
notifications = request.user.notifications.all()
paginator = Paginator(notifications, 20) # Show 20 notifications per page
page = request.GET.get('page')
try:
typesets = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
typesets = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
typesets = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'typesets': typesets})
您想显示特定用户的通知列表,对吗?然后,您应该从get\u queryset
方法返回通知列表
我认为您应该这样做,在我看来,您不应该自己处理分页,django/django restframework应该在内部处理它。
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
return self.request.user.notifications.all()
使用Django Rest框架,您只需要在设置文件中添加默认分页器,类似于这样
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}
或者,如果要使用现有代码,可以尝试以下方法:
def listing(request):
notifications = request.user.notifications.all()
paginator = Paginator(notifications, 20) # Show 20 notifications per page
page = request.GET.get('page')
try:
typesets = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
typesets = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
typesets = paginator.page(paginator.num_pages)
return render(request, 'list.html', {'typesets': typesets})
你能解决你问题中的缩进问题吗?由此可知,qs
的类型是什么?这是一个QuerySet
?你能解决你问题中的缩进问题吗?那么,这里的qs
是什么类型的?那是一个查询集
?