views:

197

answers:

2

hello 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!

+4  A: 

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

Lakshman Prasad
+3  A: 

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 %}
Tomasz Zielinski
well, it sounds good, and the tutorial is really nice, but where can i get the pagination middleware?My error is:Error importing middleware pagination.middleware: "No module named pagination.middleware"
dana
You should add it to setting.py, MIDDLEWARE_CLASSES like this:MIDDLEWARE_CLASSES = ( 'pagination.middleware.PaginationMiddleware',)In other word it's in pagination folder, middleware.py file
Tomasz Zielinski
+1 to use django-pagination :)
eos87
yes, sorry for not being accurate enough, i used 'pagination.middleware.PaginationMiddleware', in my MIDDLEWARE_CLASSES, and included paginated in my installed apps, just like in the tutorial, but i get that error:ImproperlyConfigured: Error importing middleware pagination.middleware: "No module named pagination.middleware"Were am i wrong?thanks
dana
Did you install django-pagination?`pip install django-pagination` should work
Zach
Or `easy_install django-pagination`
Tomasz Zielinski
yes that was the trick! thanks so much, guys!
dana