我正在尝试使用 Django 构建电子商务应用程序。我正在尝试使用 select tag in HTML
添加价格过滤器。因此,每次 select
option
更改时,我都想更改为按低到高过滤产品,反之亦然。
HTML 代码
<div class="d-flex flex-row-reverse" style="margin-right: 20px; margin-bottom: 20px;">
<label>
<select class="bg-dark" name="price" id="priceFilter" onchange="getValue()">
<option value="normal">Sort by Price</option>
<option value="low">Low to High</option>
<option value="high">High to Low</option>
</select>
</label>
</div>
javascript
function getValue() {
let option = document.getElementById('priceFilter');
let value = option.value;
let url = '/price_filter/';
fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({'value': value})
})
.then((response) => {
return response.json()
})
.then((data) => {
console.log('data:', data)
location.reload()
})
}
views.py
def filterByPrice(request):
data = json.loads(request.body)
value = data['value']
print(value)
if value == 'low':
products = Product.objects.all().order_by('productDiscountedPrice')
context = {
'products': products
}
return render(request, "Amazon/filteredResult.html", context)
elif value == 'high':
products = Product.objects.all().order_by('-productDiscountedPrice')
context = {
'products': products
}
return render(request, "Amazon/filteredResult.html", context)
else:
products = Product.objects.all()
context = {
'products': products
}
return render(request, "Amazon/filteredResult.html", context)
所以,问题是,它没有被重定向到 filteredResult
页面,而是在 value
中打印出来的 terminal
。
知道为什么它没有被重定向吗?