Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django basic pagination problem

i have a microblog app, and i'm trying to paginate the entries, to show only 10 per page, for example. though i've followed the tutorial, my pagination doesn't seem t be working.

the listing function looks like that:

def listing(request):
    blog_list = Blog.objects.all()
    paginator = Paginator(blog_list, 10)
    try:
        page = int(request.GET.get('page','1'))
    except ValueError:
        page = 1
    try:
        posts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        posts = paginator.page(paginator.num_pages)

    return render_to_response('profile/publicProfile.html', {"posts": posts})

and in my template:

    <div class="pagination">
<span class="step-links">
    {% if posts.has_previous %}
        <a href="?page={{ posts.previous_page_number }}">previous</a>
    {% endif %}

    <span class="current">
        Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
    </span>

    {% if object.has_next %}
        <a href="?page={{ posts.next_page_number }}">next</a>
    {% endif %}
</span>

thanks!

like image 318
dana Avatar asked Apr 11 '26 20:04

dana


1 Answers

You can use django-pagination which makes it possible to implement pagination without writing a single line of Python code, you only pass list of all objects to template (i.e. blog_list = Blog.objects.all() in your case), and then use three tags in you template:

 {% load pagination_tags %}
 {% autopaginate blog_list 10 %}
 {% paginate %}
like image 79
Tomasz Zieliński Avatar answered Apr 13 '26 09:04

Tomasz Zieliński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!