Search code examples
django-pagination

django-pagination user selected records per page


I've implemented django-pagination in my app to take care of pagination, and it's all good. The one thing I can't figure out is how to allow users to choose a certain value for the records per page e.g. 15, 30, 45, 60, 100 or all records.

What's the correct way to go about this?


Solution

  • When you construct a Paginator, you pass in the number of records per page. You need to get this value from the user, store it, and pass it in.

    For example, it could be a user setting -- if so, add it to your user setting Model, collect it in a form, store it, then get the Model from the db before creating the Paginator and pass it in.

    Or it could just be a session variable that is set from an element on the page -- again, put the element on the page, use it to set the session variable, and pass it into the Paginator object.

    Looks like it was covered in this question:

    Flexible pagination in Django

    EDIT: Reading the comment.

    If you read the source,

    http://code.google.com/p/django-pagination/source/browse/trunk/pagination/templatetags/pagination_tags.py

    The tag, autopaginate takes a number or a template variable.

        if isinstance(paginate_by, int):
            self.paginate_by = paginate_by
        else:
            self.paginate_by = template.Variable(paginate_by)
    

    So, try passing in a template variable in your view 'page_by':10 and the use {% autopaginate list page_by %} -- then you can set the page_by from the query string, db, or whatever you want.