Django how to show paginations correctly for each user?

Right now my paginations is same for all user. Let you explain. Assume I listed 2 items per page if user Jhone have 4 items then he will see total 2 page number. see the picture: enter image description here

But User Mick haven’t any items in his account why he is seeing only page numbers? see the picture

enter image description here

here is my code:

views.py

 def ShowAuthorNOtifications(request):
        user = request.user
        notifications = filters.NotificationFilter(
                          request.GET, 
                          queryset=Notifications.objects.all().order_by('-date')
                      ).qs
        paginator = Paginator(notifications, 5)
        page = request.GET.get('page')
        try:
            page_obj = paginator.page(page)
        except PageNotAnInteger:
            page_obj = paginator.page(1)
        except EmptyPage:
            page_obj = paginator.page(paginator.num_pages)
        
        notification_user =  Notifications.objects.filter(user=user).count()
        Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
        template_name ='blog/author_notifications.html'
        
        context = {
            'notifications': notifications,
            
            'notification_user':notification_user,
            'page_obj':page_obj,
        }
        print("##############",context)      
        return render(request,template_name,context)

#html

{% for notification in page_obj.object_list  %} 
{%if user.id ==  notification.blog.author.id %}
#my code
{%endif%}
{%endfor%}

#my pagination code:

<!-- Pagination-->       
 <ul class="pagination justify-content-center mb-4">
    {% if page_obj.has_previous %}
    <li class="page-item"><a class="page-link" href="?page=1">First Page</a></li>
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">← Back</a></li>
    {% endif %}

    {% if page_obj.has_next %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next Page →</a></li>
    
    {% endif %}

    {% for i in  page_obj.paginator.page_range %}
    {% if page_obj.number == i %}
    <li class="page-item"><a class="page-link" href="#!">{{ i }}</a></li>
      
    {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
    <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
    {%endif%}
    {% endfor %}
     
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last Page</a></li>
</ul>


</div>

If tried to use pagination inside my forloop but it’s showing page number after each items. How to show paginations correctly for each user?

The mistake here is building the paginator based on a queryset that retrieves all rows instead of building it based on the queryset being used to just fetch the rows for that specific user.

1 Like

How to write right quaryset so user can only see their object in pagenation page ???

Review the Retrieving Objects docs.
(It looks like you might already have the query you want in your view(?))

1 Like

Thanks KenWhitesell. My problems solved now. My previous queryset returing all object after applying this custom filter Notifications.objects.all().filter(user=user).order_by('-date') my problem solved