I've a small Django project basic-pagination. There is one model, one form and two views (list view, form view). User submits data in the form view, then the data is displayed in the list view. Pagination is enabled to only display 5 posts at a time.
What I've implemented is a form with a GET response to get the data I want to display (e.g. name, date). See code below
class FormListView(ListView):
model = models.ToDoList
paginate_by = 5 # if pagination is desired
template_name = 'pagination/listview.html'
context_object_name = 'forms'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Form List View'
context['filter_form'] = forms.FilterListView(self.request.GET)
return context
def get_queryset(self):
queryset = models.ToDoList.objects.all().order_by('-id')
name = self.request.GET.get('name')
if name:
queryset = queryset.filter(name=name)
order_by = self.request.GET.get('order_by')
if order_by:
queryset = queryset.order_by(order_by)
print(queryset)
return queryset
The problem is that the class based view ListView
calls the method get_queryset
if you are moving from page 1
to page 2
and thus losing the filtered queryset I wanted.
How can I maintain the filtering throughout the pagination process?