I have data that I am trying to display in a tabular format, and I have also added filters for that tabular data.
The filter sends an AJAX request to the server, works on the same function inside "views.py" but the page doesn't refresh with new data.
I extracted the query format of that filter and ran it through Postgres SQL which returns the exact result.
I cannot understand why it doesn't refresh the page and load the filtered data.
def productViews(request):
allproduct = Products.objects.order_by('-Memory', '-Core_Speed', '-Boost_Clock')
###########################################################################################
# this code works #
if request.method == 'POST' and 'Boost_Clock_min' in request.POST:
Boost_Clock_minimum = request.POST.get('Boost_Clock_min')
allproduct = allproduct.filter(Boost_Clock__gte=Boost_Clock_minimum)
if request.method == 'POST' and 'Boost_Clock_max' in request.POST:
Boost_Clock_maximum = request.POST.get('Boost_Clock_max')
allproduct = allproduct.filter(Boost_Clock__lte=Boost_Clock_maximum)
if request.method == 'POST' and 'Core_Clock_min' in request.POST:
Core_Clock_minimum = request.POST.get('Core_Clock_min')
allproduct = allproduct.filter(Core_Speed__gte=Core_Clock_minimum)
if request.method == 'POST' and 'Core_Clock_max' in request.POST:
Core_Clock_maximum = request.POST.get('Core_Clock_max')
allproduct = allproduct.filter(Core_Speed__lte=Core_Clock_maximum)
if request.method == 'GET' and 'ram[]' in request.GET and request.is_ajax():
ram_list = request.GET.getlist('ram[]')
ram_list = [int(i) for i in ram_list]
print("The ram sizes are \t")
print(ram_list)
allproduct = allproduct.filter(Memory__in=ram_list)
###########################################################################################
#print(request.POST)
print(request.GET)
#print(str(allproduct.query)) # This returns the query that is being rendered
# print(allproduct[0].name) # This returns the first product inside the query
print(allproduct.count())
# for x in
count_of_products = allproduct.count()
i = 0
# while i<= 5 and i< count_of_products:
# print(allproduct[i].name)
# i+=1
for card in allproduct:
if i>5:
break
else:
i+=1
#print(str(Products.objects.values('Memory').distinct().order_by('-Memory').query))
print(str(allproduct.query))
context = {
'count_of_products': count_of_products,
'product': allproduct,
'Manufacturer': Products.objects.values('Manufacturer').distinct().order_by('-Manufacturer'),
'Memory': Products.objects.values('Memory').distinct().order_by('-Memory')
}
return render(request, 'Products.html', context)
$(document).ready(function () {
filter_data();
function filter_data() {
var brand = get_filter('brand');
var ram = get_filter('ram');
$.ajax({
url: '{% url "products" %}',
method: "GET",
data: { brand: brand, ram: ram, },
success: function (data) {
console.log("Data was succesfully captured");
}
});
}
function get_filter(class_name) {
var filter = [];
$('.' + class_name + ':checked').each(function () {
filter.push($(this).val());
});
console.log(filter)
return filter;
}
$('.common_selector').click(function () {
filter_data();
});