开发者

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 }}.
 开发者_StackOverflow中文版   </span>

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

thanks!


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


Return the object_list generic view that takes the paginate_by argument, rather than return the render_to_response

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜