基于分页器 Class 的视图在 DJANGO 中不起作用

Paginator Class based views not working in DJANGO

我不确定为什么分页器不工作。它确实显示在页面底部,但不适用于 "Entity" table.

中的列表对象

我的 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>

如有任何帮助,我们将不胜感激。

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

但是你在这里太多了。事实上,ListView [Django-doc] 被定义为为您删除所有样板代码,否则 ListView 将毫无用处:

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

    def get_queryset(self):
        return super().get_queryset()<b>.filter(owner=self.request.user)</b>

Django 会自动对 get_queryset [Django-doc] and associate it with the context_object_name [Django-doc] 的结果进行分页(这里是 'entities')。

通过指定 model = Entity [Django-doc],我们可以很容易地用不同的模型重用上面的模型。

我觉得应该是这样的

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