ListView 中多个上下文中特定上下文的分页在 Django 中不起作用?

pagination of a specific context in multiple context in ListView not working in django?

在 Django 中,我试图列出几个对象的一些查询,例如用户列表、类别和 Post 列表。主页将包含几个块或框。每个框将有不同的查询列表,如 Post 列表、用户列表、类别列表。但是只有一个上下文会有分页,其他上下文不会,当然分页将在 Post 列表上工作。

这里是 views.py:

class BlogappListView(ListView):
    model = Category,CustomUser
    template_name = 'home.html'
    context_object_name='category_list'
    queryset=Category.objects.all()
    paginate_by = 2

    def get_context_data(self, **kwargs):
        context = super(BlogappListView, self).get_context_data(**kwargs)
        context['user_list']= CustomUser.objects.all()
        context['post_list']=Post.objects.all()
        activities=context['post_list']
        return context
    def get_related_activities(self,activities):
        queryset=self.objects.activity_rel.all()
        paginator=Paginator(queryset,2)
        page=self.request.GET.get('page')
        activities=paginator.get_page(page)
        return(activities)

在urls.py中:

urlpatterns = [
    path('',BlogappListView.as_view(),name='home'),
]

在base.html中分页部分代码:

<ul class="pagination justify-content-center mb-4">  
              {% if is_paginated %}

                    {% if page_obj.has_previous %}
                    <li class="page-item">  
                    <a class="page-link" href="?page=1">First</a>
                  </li>
                  <li class="page-item">
                      <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
                    </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                      {% if page_obj.number == num %}
                      <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                      </li>

                      {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                      <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                      </li>  

                      {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                    <li class="page-item">
                      <a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
                    </li>
                  <li class="page-item">
                      <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
                    </li>

                    {% endif %}

                  {% endif %}
                </ul>

主要问题是第 1 页显示所有帖子以及第二页。类别列表也被截断为 2,但用户列表正常。

那么我怎样才能让它发挥作用呢?还有其他办法吗?

提前致谢

我解决了。修改后的代码是:

class BlogappListView(ListView):
    model = Category,CustomUser
    template_name = 'home.html'
    context_object_name='post_list'
    queryset=Post.objects.all()
    paginate_by=2

    def get_context_data(self, **kwargs):
        context = super(BlogappListView, self).get_context_data(**kwargs)
        context['user_list']= CustomUser.objects.all()
        context['category_list']=Category.objects.all()
        return context

在寻找解决方案时,我在某个地方写到分页仅适用于查询集,不适用于上下文。这很容易理解,当我在 Post 下的查询集中使用 paginate_by=2 时,它只适用于此。

解决方案快速而简短,但我花了一段时间才弄清楚哪个不好。但无论哪种方式,感谢这个平台进行这个讨论,我保留这个解决方案,以便有人可以从中获得帮助。