Q:

Dynamic pagination using Generic ListView

I have a requirement that a user should be able to choose how many items per page he wants to see in a list of assistants. I am using a generic ListView in Django 1.4.
My code before this change looks like this:
Class AssistantList(ListView):
    template_name = "register/assistant_list.html"
    context_object_name = 'assistant_list'
    paginate_by = 25

How can I set the paginate_by dynamically based on the user's selection instead of hardcoding it as above?

A:

I just recently had to do this. You can override the get_paginate_by function to grab a query string parameter.  Here's a basic example.
Class AssistantList(ListView):
    template_name = "register/assistant_list.html"
    context_object_name = 'assistant_list'
    paginate_by = 25

    def get_paginate_by(self, queryset):
        """
        Paginate by specified value in querystring, or use default class property value.
        """
        return self.request.GET.get('paginate_by', self.paginate_by)

Then in our html we have a dropdown selection where the user can choose the number of items to view.
<form action="." method="get">
    <select name="paginate_by">
        <option>25</option>
        <option>50</option>
        <option>75</option>
        <option>100</option>
    </select>
</form>

We added some javascript to make it auto-submit and you'll want to pass the paginate_by through to the context so you can make sure it continues to get passed from page to page.

