django中的价格范围过滤器



我想按最低价格到最高价格的价格范围筛选产品。我试过了,但我得到了所有的产品。请建议我怎么做?

def search(request):
price_from = request.GET.get('price_from', None)
price_to = request.GET.get('price_to', None)
products = Product.objects.filter(price__range=(price_from,price_to))

context = {
'price_from': price_from,
'price_to': price_to,
'products': products,
}

return render(request, 'search.html', context)

我的模板代码

<div class="range-slider">
<form method="get" action="{% url 'search' %}" class="d-flex align-items-center justify-content-between">
<div class="price-input">
<label for="amount">PriceFrom </label>
<input type="number" name="price_from"  value="{{price_from}}">
</div>
<div class="price-input">
<label for="amount">PriceTo</label>
<input type="number" name="price_to"  value="{{price_to}}">
</div>
<button class="filter-btn">filter</button>
</form>
</div>
<div class="range-slider">
<form method="get" action="{% url 'search' %}" class="d-flex align-items-center justify-content-between">
<div class="price-input">
<label for="amount">PriceFrom </label>
#Just Apply For loop for evry price form
{% for i in price form %}
<input type="number" name="price_from"  value="{{price_from}}">
{% end for %}
</div>
<div class="price-input">
<label for="amount">PriceTo</label>
{% for i in price to %}
<input type="number" name="price_to"  value="{{price_to}}">
{% end for %}
</div>
<button class="filter-btn">filter</button>
</form>
</div>
</div>

告诉我这对你是否有效

最新更新