0

I am using two query sets in one page and I want to have different paginations for each one

this is my class based view:

class MainListView(ListView):
    queryset = Post.objects.all()
    template_name = 'main.html'
    context_object_name = 'posts'

    def get_context_data(self, **kwargs):
        context = super(MainListView, self).get_context_data(**kwargs)
        context['news'] = EventsPost.objects.all()
        context['posts'] = self.queryset
        return context

I want the Post model to be paginated by 3 and EventsPost by 6 Thanks for your responses

Sam
  • 379
  • 2
  • 10
  • this is my home page and init i have to show the latest posts and events beside having individual pages for them. what if i use function base view? i don't get it actually – Sam Apr 23 '20 at 10:22
  • Do you actually need pagination (i.e. show page 2 of posts on the homepage). Or do you just want to show the latest 3 posts and 6 event posts? The second option is much simpler. – Alasdair Apr 23 '20 at 10:49
  • second option is literally what i want! – Sam Apr 23 '20 at 10:51

1 Answers1

1

Since you don't need pagination, I would use a TemplateView, and add the two querysets to the context in the get_context_data method.

class MainListView(TemplateView):

    def get_context_data(self, **kwargs):
        context = super(MainListView, self).get_context_data(**kwargs)
        context['news'] = EventsPost.objects.order_by('-pk')[:6]
        context['posts'] = Post.objects.order_by('-pk')[:3]
        return context

If you want to sort on a different field, you could change it to something else e.g. order_by('-date')

Alasdair
  • 298,606
  • 55
  • 578
  • 516