Paginacion does not respect filters in django

0

Hello, I hope you are super, it happens that I am new to django and I am making a page in a listview. Up there all right, add a search filter by dates and filter without problem. But when I use the next page I have done the filter, it shows me all the records. Thank you in advance

    
asked by Edgardo Zelada 18.06.2018 в 18:55
source

1 answer

0

I've attached a piece of code to you to modify it according to your code.

View

class ProductoFiltrar(FilterView):
    model = Producto
    filterset_class = ProductoFilter
    paginate_by = '50'
    is_paginated = True

def get_context_data(self, **kwargs):
     context = super(ProductoFiltrar, self).get_context_data(**kwargs)
     parametros = self.request.GET.copy()
     if parametros.get('page') != None:
        del parametros['page']
        context['parametros'] = parametros
     return context

Template

{% if is_paginated %}
    <ul class="pagination">
    {% if page_obj.has_previous %}
        <li>
            <span><a href="?page={{ page_obj.previous_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}">Anterior</a></span>
        </li>
    {% endif %}
        <li class="">
            <span>Página {{ page_obj.number }} de {{ page_obj.paginator.num_pages }}.</span>
        </li>
    {% if page_obj.has_next %}
        <li>
            <span><a href="?page={{ page_obj.next_page_number }}{% if parametros.urlencode %}&{{ parametros.urlencode }}{% endif %}">Siguiente</a></span>
        </li>
    {% endif %}
    </ul>
{% endif %}

This will copy the filter parameters and remove the page parameter to overwrite it with the new page.

FilterView is a class of a library called django-filter , as I do not know how you have created your view I attached the library. django-filter

    
answered by 19.06.2018 в 11:52