Django фильтр цена мин/макс диапазон
Пытаюсь отфильтровать цену на сайте на основе диапазона min max.
Мое мнение
def products(request, slug):
"""
List all products of a category
"""
category = get_object_or_404(Category, slug=slug)
products = Product.objects.filter(category=category)
brand_queryset = Brand.objects.filter(products__in=products).distinct('name')
# Filter products based on price range
if 'min_price' in request.GET:
min_price = request.GET['min_price']
max_price = request.GET['max_price']
print("Min price: {}Max price: {}".format(min_price, max_price))
if not max_price:
max_price = Product.objects.all().aggregate(Max('price'))['price__max']
products = products.filter(price__range=(min_price, max_price))
context = {
'products': products,
'category': category,
'form': form,
'brand_queryset': brand_queryset,
'title': Product,
}
return render(request, 'products/products.html', context)
Html
<div class="card-body">
<div class="form-row">
<div class="form-group col-md-6">
<label>Min</label>
<input cass="form-control" name="min_price" value="min_price" placeholder="Kr 0" type="number">
</div>
<div class="form-group text-right col-md-6">
<label>Max</label>
<input class="form-control" name="max_price" value="max_price" placeholder="Kr 1,0000" type="number">
</div>
</div>
<button class="btn btn-block btn-primary" type="submit" value="Go">Apply</button>
</div>
Пытаюсь отсортировать список продуктов на основе диапазона цен, min и max. Показывать только те товары, которые находятся в этом диапазоне. Однако мне трудно добиться совместной работы представления и html.