Home > database >  Django pagination in ListView raise Error "Slice"
Django pagination in ListView raise Error "Slice"

Time:02-02

i want display pagination on my page but i noticed that when i add "paginate_by" raise me error "slice"

class BookNotLogged(generic.ListView):
     template_name="BookNotLogged.html"
     context_object_name='listofBook'
     paginate_by = 2 #when i add this its raise me error
     model = Book
     queryset = Book.objects.all()

     def get_queryset(self):
         context={
                'book1':Book.objects.get(id="2"), 
                'book2':Book.objects.get(id="3"),
                'book3':Book.objects.get(id="4"),
                'book4':Book.objects.get(id="8"),
                'book5':Book.objects.get(id="9"),
                'book6':Book.objects.get(id="10"),
                'book7':Book.objects.get(id="11"),
                'book8':Book.objects.get(id="12"),
                'book9':Book.objects.get(id="13") 
         }
    
         return context

CodePudding user response:

The get_queryset is used to return customized queryset not the context. To get context you have to override get_context_data so rename get_queryset with get_context_data like this:

class BookNotLogged(generic.ListView):
    # rest of code

    def get_context_data(self, **kwargs):
          context = super().get_context_data(**kwargs)
          context.update({
            'book1':Book.objects.get(id="2"), 
            'book2':Book.objects.get(id="3"),
            'book3':Book.objects.get(id="4"),
            'book4':Book.objects.get(id="8"),
            'book5':Book.objects.get(id="9"),
            'book6':Book.objects.get(id="10"),
            'book7':Book.objects.get(id="11"),
            'book8':Book.objects.get(id="12"),
            'book9':Book.objects.get(id="13"),
         })

        return context
  •  Tags:  
  • Related