I want to make a checkbox type filter on the items of an e-commerce

I have a simple e-commerce where I want to filter items. This will be done in a form checkbox, but the form's categories are user-created, ie it's a "for" of Cor.objects.all() . The filter only works for the last selected color. So I wanted it to have multiple filters.

index.html:

              <div class="widgets-item">
                  <form id="widgets-checkbox-form" action="{% url 'carro_filtro' %}" method="GET">
                    <ul class="widgets-checkbox">
                      {% for cor in cores %}
                      <li>
                          <input class="input-checkbox" type="checkbox" id="color-selection-{{ cor.id }}" name="termo" value="{{ cor.nome_cor }}">
                          <label class="label-checkbox mb-0" for="color-selection-{{ cor.id }}">
                            {{ cor.nome_cor }}
                          </label>
                      </li>
                      {% endfor %}
                    </ul>
                    <input type="submit" class="btn btn-custom-size lg-size btn-primary w-100 mb-5 mt-5" value="Filtrar">
                  </form>
              </div>

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.CarView.as_view(), name='shop'),
    path('filtro/', views.CarroFiltro.as_view(), name='carro_filtro'),
]

views.py

class CarView(ListView):
    model = Car
    template_name = 'shop/index.html'
    paginate_by = 12
    context_object_name = 'cars'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['personalizacoes'] = Shop.objects.filter(
            publicado_shop=True).order_by('-id').first()
        context['categorias'] = Categoria.objects.all()
        context['cores'] = Cor.objects.all()

        return context

    def get_queryset(self):
        qs = super().get_queryset()
        categoria = self.kwargs.get('nome_categoria', None)

        if not categoria:
            qs = qs.filter(publicado=True).order_by('-id')
            return qs

        qs = qs.filter(
            categoria_carro__nome_categoria__iexact=categoria, publicado=True).order_by('-id')
        return qs


class CarroFiltro(CarView):

    def get_queryset(self):
        qs = super().get_queryset()
        color_selection = self.request.GET.get('termo')

        print("X"*100)
        print(color_selection)
        print("X"*100)

        if not color_selection or color_selection is None:
            return qs

        qs = qs.filter(
            Q(modelo__icontains=color_selection) |
            Q(marca__nome_marca__icontains=color_selection) |
            Q(ano__icontains=color_selection) |
            Q(cor__nome_cor__icontains=color_selection) |
            Q(quilometragem__icontains=color_selection) |
            Q(motorizacao__icontains=color_selection) |
            Q(cambio__nome_cambio__icontains=color_selection) |
            Q(categoria_carro__nome_categoria__icontains=color_selection)
        )

        return qs

I want to make multiple filters. In the View "CarroFiltro" I printed the GET, but it only has the last checkbox selected. I want it to have all checkboxes selected.

Back to Top