基于Paginator类的视图在DJANGO中不起作用

时间:2019-06-06 14:58:04

标签: django python-3.x django-views django-class-based-views paginator

我不确定为什么分页器无法正常工作。它确实显示在页面底部,但不适用于“实体”表中的列表对象。

我在html或视图中是否做错了什么?

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>

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

问题是您将queryset作为entities传递给渲染器,而不是page_obj.object_list [Django-doc],后者是要渲染的元素的集合。

但是您在这里自己做了太多 。实际上,已经定义了ListView [Django-doc]来为您删除所有样板代码,否则ListView将会毫无用处:

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将自动对get_queryset [Django-doc]的结果进行分页,并将其与context_object_name [Django-doc](此处为'entities')相关联。

通过指定model = Entity [Django-doc],我们可以轻松地将上述模型与其他模型重复使用。

答案 1 :(得分:2)

我认为应该是这样

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

我也想这样会更简单:

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