Curtis Banks
Curtis Banks

Reputation: 362

Paginator Class based views not working in DJANGO

I am not sure why the paginator is not working. It does show at the bottom of the page but it does not apply to the list objects from "Entity" table.

Is there something i am doing wrong in my html or views?

models.py

class Entity(models.Model):
    entity_name = models.CharField(max_length=250, blank=False)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='users')
    slug = models.SlugField(max_length=250, unique=True, null=True)
    active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name_plural = "Entities"

    def __str__(self):
        return self.entity_name

    def get_absolute_url(self):
        return reverse('snippets:password', kwargs={'pk': self.pk})

views.py

class EntityListView(LoginRequiredMixin, ListView):

    template_name = 'snippets/list.html'

    def get(self, request):

        queryset = Entity.objects.filter(owner=request.user)

        paging = queryset

        paginator = Paginator(paging, 3) 

        page = request.GET.get('page')

        page_obj = paginator.get_page(page)

        return render(request, self.template_name, context={
                                                'entities':queryset,
                                                'page_obj':page_obj
                                                })

html.py

{% for entity in entities %}

        <a href='{% url "snippets:password" entity.pk %}'>{{ entity.entity_name }}</br></br></br></a>

      {% endfor %}


      <div class="pagination">
          <span class="step-links">
              {% if page_obj.has_previous %}
                  <a href="?page=1">&laquo; first</a>
                  <a href="?page={{ page_obj.previous_page_number }}">previous</a>
              {% endif %}

              <span class="current">
                  Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
              </span>

              {% if page_obj.has_next %}
                  <a href="?page={{ page_obj.next_page_number }}">next</a>
                  <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
              {% endif %}
          </span>
      </div>

Any help would be much appreciated.

Upvotes: 2

Views: 535

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

The problem is that you pass the queryset as entities to your renderer, not the page_obj.object_list [Django-doc], which is a collection of elements to render.

But you are doing too much yourself here. In fact a ListView [Django-doc] is defined to remove all that boilerplate code for you, otherwise the ListView would be quite useless:

class EntityListView(LoginRequiredMixin, ListView):
    model = Entity
    template_name = 'snippets/list.html'
    context_object_name = 'entities'
    paginate_by = 3

    def get_queryset(self):
        return super().get_queryset().filter(owner=self.request.user)

Django will automatically paginate the result of get_queryset [Django-doc] and associate it with the context_object_name [Django-doc] (here 'entities').

By specifying the model = Entity [Django-doc], we can easily reuse the above model with a different model.

Upvotes: 2

flakavir
flakavir

Reputation: 200

i think should be like this

return render(request, self.template_name, context={'entities':page_obj,'page_obj':page_obj})

also i think will be more simple like this:

class EntityListView(ListView):
  model = Entity
  context_object_name = 'entities'
  paginate_by = 3

Upvotes: 2

Related Questions