@fronttrty

Как выбранные чеккбоксы оставить после применения фильтра?

сделал фильтр но при применении выбранные чекбоксы перестают быть таковыми.И как если фильтр пуст вывести весь каталог.
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"),
]
  • Вопрос задан
  • 160 просмотров
Пригласить эксперта
Ответы на вопрос 1
tumbler
@tumbler Куратор тега Django
бекенд-разработчик на python
В верстке проставить "checked" или его аналог в зависимости от того, что пришло в форму фильтра. Ну или в сам запрос, раз вы без форм и django-filter обошлись.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы