Search code examples
djangodjango-viewsdjango-templatesdjango-paginationdjango-context

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


in django, i am trying to list some queries of several objects like user lists, categoies and Post list. the homepage will be contained couple of blocks or boxes. each box will have different query list like Post list, User List, category list. But only one context will have pagination and other won't and ofcourse the pagination will be working on Post list.

here is the 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)

in urls.py:

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

in base.html, the paginate portion code:

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

the main problem is that page number 1 is showing all the posts and also the second page. and category list is also truncated into 2 but the user list ok.

so how can i make it work ? is there any other way?

thank you in advance


Solution

  • i solved it. the revised code is:

    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
    

    while looking for solution, i've got somewhere written that pagination will work only with queryset, not with the context. and that was so straight cut to understand and when i use paginate_by=2 in the queryset under Post it will only work for that.

    the solution was quick and short but it took me a while to figure it out which is not good. but either way thanx to this platform to have this discussion and i am keeping this solution so that someone might get help from it.