I'm learning to use a paginator as my list are getting really long! I'm having trouble implementing it as I am getting an error. I notice that if I remove paginate_by = 10
then the error goes away, but I'm pretty sure this is necessary to paginate. Why is the get_queryset conflicting with the 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)
Traceback:
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()
You want to display the list of notification for a particular user, right ? Then you should return list of notifications from get_queryset
method.
I think you should something like this and from my point of view you shouldn't handle pagination yourself, django/Django-restframework should handle it internally.
class NotificationsListView(ListView):
template_name = "notices/list.html"
paginate_by = 10
def get_queryset(self, *args, **kwargs):
return self.request.user.notifications.all()
With Django Rest framework you only need to add default paginator in your settings file, something like this.
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 50,
}
Or if you want to use existing code then you can try something like this :
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})