I've got a django app that has a filter as one of it's features.
The filter values are decided from a checkbox which is sent to the django backend using ajax as follows:
$('input.type-check').on('click', function(){
var a = $(this).prop('checked');
if(a == true){
elem.push($(this).attr('data-role'));
}else{
elem.splice($(this).attr('data-role'));
}
var cats = '';
$.each(elem, function(i){
cats += elem[i];
});
var xurl ='/filter?category='+cats;
$.ajax({
type: 'GET',
url: xurl,
success: function(data){
$('div.products').html(data);
}
})
});
The /filter$' url is mapped to thefitlered` view which is:
def filtered(request):
if 'category' in request.GET and request.GET['category']:
cat = request.GET['category']
ct = Product.objects.filter(category__in=cat)
diction = {'prods': ct}
return render(request, 'filter.html', diction)
It works when only one category is sent as parameter. However, when I send multiple, it gives no results.
Eg:
filter?category=Dairy will return the product that's associated with that category. However, filter?category=Dairy,Plastics or filter?category=DairyPlastics (which is from the above mentioned Javascript snippet) returns no result.
I've tried putting the category inside brackets in the view as follows [cat] but that doesn't help either. What should I do to make it return results?