How to apply paging in Django's formview?

Issue

my view.py

class Formtestview(FormView): 
    template_name = 'test.html'
    form_valid(self, form): 
           'my code to search' => 'result is object_list'
            page = self.request.GET.get('page','1')
            paginate = Paginator(object_list, 10)
            page_obj = paginator.get_page(page)
            return render(self.request, 'test.html', {'form':self.form_class, 'object_list':object_list, 'page_obj' = page_obj})

As in the code above, input is received through form_valid, paged, and then sprayed on the same html template.

Results and paging are also displayed normally, but if you go to another page like ?page=2, only the basic template without all the results is shown.

Is there a way to paginate the form and search results in one template?

Solution

Why did you use FormView? Pagination comes with MultipleObjectMixin. For simplicity, you can use ListView rather than FormView. ListView is inherited from MultipleObjectMixin.

This example is from docs:

from django.views.generic import ListView

from myapp.models import Contact

class ContactListView(ListView):
    paginate_by = 2
    model = Contact

Answered By – black

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published