Paginate persist object in search data ListView

17 views
Skip to first unread message

flaudizio

unread,
Aug 10, 2016, 10:34:07 PM8/10/16
to Django users
Hi,

I've a problem with paginate in CBV using a form search to filter results.

When I get "page 2" (in paginator) the object_list (filtered by name example) load all data again.

I've a object with 20 records, so I get a search and returned 12 records (paginate_by=2) generate 2 pages.
But when I go to the page 2 my object get again all 20 records

I'd like to "persist" the object

My View:

class EstadoListView(LoginRequiredMixin, ListView):

    model = Estado
    context_object_name = 'estados'
    paginate_by = settings.PAGINATE_BY
    template_name = 'estado_list.html'

    def post(self, request, *args, **kwargs):
        pesquisa = self.request.POST['keyword']
        self.object_list = Estado.objects.pesquisa(pesquisa) #filtering the object
        return self.render_to_response(self.get_context_data(object_list=self.object_list))

    def get_queryset(self):
        result = super(EstadoListView, self).get_queryset()

        if self.request.POST.get('keyword'):
            pesquisa = self.request.POST.get('keyword')
        else:
            pesquisa = None

        if pesquisa:
            result = Estado.objects.pesquisa(pesquisa)

        return result

My form (template):

<form method="post" class="form-inline" action=".">{% csrf_token %}
                            <div class="input-group">
                                <div class="input-cont">
                                    <input type="text" name="keyword" placeholder="Digite para pesquisar..." class="form-control"/>
                                </div>
                                <span class="input-group-btn">
                                <button type="submit" class="btn green-meadow">
                                Pesquisar &nbsp; <i class="m-icon-swapright m-icon-white"></i>
                                </button>
                                </span>
                            </div>
                        </form>

Thank's a lot
Regards,
Flaudizio Filho
Reply all
Reply to author
Forward
0 new messages