Paginator broken

Hello Django Community,

I am facing a problem using Paginator. All news are appearing on each page. The paginator looks broken also.

Could you help me with this one please.

Thank you

Here you can see the result: https://streamable.com/6r2a8p

!

Screenshot 2020-07-07 at 11.31.17

First, when sharing code, please do not post images. They’re not always readable on every device someone may be using to read these posts.

When you have code (or output) to share, enclose the text between two lines consisting only of three backtick (`) characters. (Make sure you use the backtick ` and not the apostrophe ')

Also label the block with the name of the file being included.

It looks like your view includes two custom mixins in addition to the standard ListView. Please post the code for those mixins - that’s going to be necessary to figure out what might be going wrong.

Good morning Mr KenWhitesell,

Thanks for your reply.

Sorry for my post.

As a new member I was allowed to post only one image and I didn’t knew how to share my code. Now I know thanks.

These are my mixins:

 from django.views.generic import ListView, DetailView
 from .models import News
 from django.core.paginator import Paginator
 from pages.models import Footer



 class MultipleObjectsMixin(object):
     def get_context_data(self, **kwargs):
         ctx = super().get_context_data(**kwargs)
         ctx['limited_news_list'] = News.objects.filter(status=1).order_by('-date')[:3]
         ctx['cookie_vars'] = Footer.objects.all().first()
         return ctx

 class NoDraftPublishedMixin(object):
     def get_context_data(self, **kwargs):
         ctx = super().get_context_data(**kwargs)
         ctx['news_list'] = News.objects.filter(status=1).order_by('-date')
         return ctx

 class NewsListView(MultipleObjectsMixin, NoDraftPublishedMixin, ListView):
     model = News
     context_object_name = 'news_list'
     template_name = 'news/news_list.html'
     paginate_by = 20


 class NewsDetailView(MultipleObjectsMixin, DetailView):
     model = News
     template_name = 'news/news_detail.html'
     slug_url_kwarg = 'slug'

Thank you again :slightly_smiling_face:

Ok, so the fundamental problem here is your mixins. You’ve written two mixins that are overriding what ListView is trying to do with your pagination.

You’re explicitly setting news_list in your get_context_data to the complete queryset instead of modifying the existing queryset, which would be the paged version, breaking the pagination logic that it would provide.

If you want to see how to write mixins that work with base classes rather than against the base class, look at some of the existing mixins from the Django code itself.

Ken

Ok. I will try this. Thank you :wink:

There is an additional thought I’ll toss out here - you’ll probably want to modify/extend the get_queryset method rather than the get_context_data method if all you’re doing in your mixin is apply the filter and order_by clauses. You want to filter and sort the data before you extract your slices for the page, not after.

Ok thank you for your help. Will give you a feedback :wink:

Happy Birthday Ken,

It’s working, I spent a lot of time understanding what you meant :wink:

My NoDraftPublishedMixin was overriding my ListView :slightly_smiling_face:

Will read more about mixins,

Thanks again :wink: