Monday, June 1, 2020

Re: Django Pagination

Dear Sir,

it shows all the text on all the pages.

if there are total 5 posts, then it  shows all the 5 posts on all the pages.

Thanks,

Akshat Zala

On Saturday, 30 May 2020 20:54:35 UTC+5:30, maninder singh Kumar wrote:
Views.py looks fine !

regards
willy


On Sat, May 30, 2020 at 7:22 PM Akshat Zala <aksha...@gmail.com> wrote:
Hello,

I am finding it difficult to paginate through all the posts

My views.py :

@login_required
def home_view(request):
"""Display all the post of friends and own posts on the dashboard"""
posts = Post.objects.all().order_by('-date_posted')
media: MEDIA_URL
# 'post':Post.objects.filter(Q(author=request.user) | Q(author__from_user=request.user) | Q(author__to_user=request.user)).order_by('-date_posted'),
paginator = Paginator(posts, 2)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
return render(request, 'post/home.html',{'page_obj': page_obj})


and in template post/home.html:

{% if is_paginated %}
{% if page_obj.has_previous %}
<a class="btn btn-outline-info mb-4" href="?page=1">First</a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.previous_page_number }}"><span class="material-icons">arrow_left</span></a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-4' and num < page_obj.number|add:'4' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.next_page_number }}"><span class="material-icons">arrow_right</span></a>
<a class="btn btn-outline-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Last</a>
{% endif %}


Thanks

Akshat Zala

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87c3f0db-3088-448c-b3c7-14450e8c2f5d%40googlegroups.com.

--
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/c775d687-0773-4d09-86be-35e6f2cd55d2%40googlegroups.com.

No comments:

Post a Comment