Monday, June 1, 2020

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/695c35d0-4841-4aaf-9080-7817cbe27fa9%40googlegroups.com.

No comments:

Post a Comment