class Matirial(models.Model):
title = models.CharField('Название', max_length=150)
url = models.SlugField(max_length=150, unique=True)
def __str__(self):
return self.title
class yarn(models.Model):
title = models.CharField('Название', max_length=150)
description = models.TextField("Описание")
price = models.PositiveSmallIntegerField("Цена", default=0)
numberInStock = models.PositiveSmallIntegerField("Количество в наличии", default=0)
color = models.ManyToManyField(Color, verbose_name="Цвет")
creator = models.CharField("Производитель", max_length=150)
img = models.ImageField("Изображение", upload_to="img/")
url = models.SlugField(max_length=150, unique=True)
matirial = models.ManyToManyField(Matirial, verbose_name="Материал")
metrag = models.CharField('Метраж', max_length=150)
def __str__(self):
return self.title
from django.urls import path
from . import views
urlpatterns = [
path('katlog/filter', views.product_list, name="filter_view"),
path("katalog/<slug:slug>/", views.YarnDetail.as_view(), name="katalog_detail"),
]
import django_filters
from .models import yarn, Matirial
class ProductFilter(django_filters.FilterSet):
price__gte = django_filters.NumberFilter(field_name='price', lookup_expr='gte')
price__lte = django_filters.NumberFilter(field_name='price', lookup_expr='lte')
matirial = django_filters.filters.ModelMultipleChoiceFilter(queryset=Matirial.objects.all())
class Meta:
model = yarn
fields = []
def product_list(request):
filter = ProductFilter(request.GET, queryset=yarn.objects.all())
return render(request, "yarn/katalog.html", {'filter': filter})
class YarnView(ListView):
model = yarn
queryset = yarn.objects.all()
template_name = "yarn/katalog.html"
<form action="{% url 'filter_view'%}" ><button type="submit"></button>
{{ filter.form.as_p }}
</form>
{% for Yarn in filter.qs %}
<div class="col-md-4 col-sm-12">
<div class="buy">
<img type="button" data-toggle="modal" data-target="#exampleModal{{Yarn.title}}" src="{{Yarn.img.url}}" alt="">
<a href="{{ Yarn.get_absolute_url }}" style="color: #212529">
<h3 class="float-left">{{Yarn.title}}</h3>
</a>
<div class="buy__text">
<div class="intocard">
<img src="https://img.icons8.com/material-two-tone/32/000000/shopping-basket-add.png" />
</div>
<div class="clearfix">
</div>
<div class="prais">
<h4>цена: {{Yarn.price}}$/г</h4>
<h4>в наличии: {{Yarn.numberInStock}}г </h4>
</div>
</div>
</div>
<div>
<div class="modal fade" id="exampleModal{{Yarn.title}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<button type="button" class="close " data-dismiss="modal" aria-label="Close">
<span aria-hidden="true"><img src="https://img.icons8.com/small/40/000000/filled-cancel.png" /></span>
</button>
<div class="modal-body">
<img src="{{Yarn.img.url}}" class="modal__img" alt="">
</div>
</div>
</div>
</div>
</div>
{%endfor%}
def product_list(request):
filter = ProductFilter(request.GET, queryset=yarn.objects.all())
return render(request, "yarn/katalog.html", {'filter': filter})
import django_filters
from .models import yarn
class ProductFilter(django_filters.FilterSet):
price__gte = django_filters.NumberFilter(field_name='pricegte', lookup_expr='gte')
price__lte = django_filters.NumberFilter(field_name='pricelte', lookup_expr='lte')
matirial = django_filters.BooleanFilter()
class Meta:
model = yarn
fields = ["price", "matirial"]