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.
The views.py of the app
```
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())
#print(allproduct.name)
# 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:
print(card.name)
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)
```
The jquery inside the template
```
$(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();
});
```
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b3c9d434-786c-4300-a0e0-1a677cdaaaca%40googlegroups.com.