I'd like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project to do this?
Thanks
I'd like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project to do this?
Thanks
google on "django pagination" and make sure to use "covering index" in your SQL for efficient query.
Django has a Paginator object built into core. It's a rather straightforward API to use. Instantiate a
Paginator
class with two arguments: the list and the number of entries per "page". I'll paste some sample code at the bottom.In your case you want to allow the user to choose the per-page count. You could either make the per-page count part of the URL (ie. your/page/10/) or you could make it a query string (ie. your/page/?p=10).
Something like...
Here's some sample code from the Django doc page for the Paginator to better see how it works.
What does need? Well.
You can add custom control for change_list.html, for pagination block for example.
This will be reload list page with get parameter per_page for example with certain value onchange event.
For your adminModel you must override changelist_view method where you must handle get parameter and set this value as list_per_page field value.
I use extra_context for access to this values into template. Maybe there is more neat approach to access i don't know :-)
T. Stone's answer covers most of what I was going to say. I just want to add that you can use pagination in Generic Views. In particular, you may find
django.views.generic.list_detail.object_list
useful.You can write a small wrapper function that gets the number of objects to display per page from the
request
object, then callsobject_list
.Then, the context for your template will contain the variables,
and you can loop through
page_obj
to display the objects for that page.