【问题标题】:pagination of a specific context in multiple context in ListView not working in django?ListView中多个上下文中特定上下文的分页在django中不起作用?
【发布时间】:2020-09-20 06:36:28
【问题描述】:

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

这里是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,但用户列表还可以。

那么我怎样才能让它工作呢?还有其他方法吗?

提前谢谢你

【问题讨论】:

    标签: django django-views django-templates django-pagination django-context


    【解决方案1】:

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

    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 时,它只会适用于此。

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

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2018-10-24
      相关资源
      最近更新 更多