F
F
fronttrty2020-08-08 22:45:20
Django
fronttrty, 2020-08-08 22:45:20

How to leave the selected checkboxes after applying the filter?

made a filter, but when applied, the selected checkboxes cease to be such. And as if the filter is empty, display the entire directory.

from django.shortcuts import render
from django.views.generic.base import View
from django.views.generic import DetailView, ListView
from .models import yarn, Matirial, Color
from django.db.models import Q

class Filter:
  def get_color(self):
    return Color.objects.all()
  def get_matirial(self):
    return Matirial.objects.all()

class YarnView(Filter, ListView):
  model = yarn
  queryset = yarn.objects.all() 
  template_name = "yarn/katalog.html"

class FilterView(Filter, ListView):
  model = yarn
  queryset = yarn.objects.all() 
  template_name = "yarn/katalog.html"
  def get_queryset(self):
    queryset = yarn.objects.filter(
    Q(matirial__in=self.request.GET.getlist("matirial"))|
    Q(color__in=self.request.GET.getlist("color"))
      )
    return queryset

<form action="{% url 'filter_name' %}" method="get">
                        <div class="accordion" id="accordionExample1">
                            <div class="">
                                <div id="headingTwo">
                                    <button class="collapsed filter__punkt main__punkt" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                        <p>
                                            Состав
                                        </p>
                                        <img class="float-right plus__menu" src="https://img.icons8.com/android/24/000000/plus.png" />
                                    </button>
                                </div>
                                <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample1">

                                    {% for matirial in view.get_matirial %}
                                        <label class="filter__punkt">
                                            <p>{{ matirial.title }}</p><input class="float-right plus__menu"  type="checkbox" name="matirial" value="{{ matirial.id }}">
                                        </label>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                        <button type="submit"> применить</button>
                        <div class="accordion" id="accordionExample">
                            <div class="">
                                <div id="headingTwo">
                                    <button class="collapsed filter__punkt main__punkt" type="button" data-toggle="collapse" data-target="#color__akardion" aria-expanded="false" aria-controls="color__akardion">
                                        <p>
                                            Цвет
                                        </p>
                                        <img class="float-right plus__menu" src="https://img.icons8.com/android/24/000000/plus.png" />
                                    </button>
                                </div>
                                <div id="color__akardion" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
                                    <div style="margin-top: 10px">
                                        {% for colors in view.get_color%}
                                        <label>
                                            <input type="checkbox" class="input__color-checkbox" name="color" value="{{ colors.id }}">
                                            <span class="custom__checkbox"> <span class="custom__checkbox-color" style="background: {{colors.colour}}"></span></span>
                                        </label>
                                        {% endfor %}
                                    </div>
                                </div>
                            </div>
                        </div>
                        </form>

from django.urls import path

from . import views

urlpatterns = [
    path('katalog', views.YarnView.as_view()),
    path('filter/', views.FilterView.as_view(), name='filter_name'),
    path("katalog/<slug:slug>/", views.YarnDetail.as_view(), name="katalog_detail"),
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-08-09
@tumbler

Put "checked" or its equivalent in the layout, depending on what came in the filter form. Well, or in the request itself, since you did without forms and django-filter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question