I'm passing list object from view to ajax autocomplete in template but unable to show the no Cars found in the drop down list
I'm passing a list object as 'No Cars Found' if no objects found in dB and if found the relevant objects are sent.But i Could see DB objects are shown in the dropdown but 'No Cars Found' is not shown in the Dropdown when no objects found.
<
script type = "text/javascript" >
jQuery(function complete() {
$(".basicAutoComplete").on('keyup', function() {
var value = $(this).val();
$.ajax({
url: "{% url 'ajax_autocomplete' %}",
data: {
'search': value
},
dataType: 'json',
success: function(data) {
var carslist = data.list;
list = carslist;
$(".basicAutoComplete").autocomplete({
source: list,
minLength: 2,
});
}
});
});
});
<
/script>
View
def autocomplete(request):
if request.is_ajax():
q=request.GET.get('search')
queryset = Cars.objects.filter(car_model__istartswith=q).values('car_model')
list = []
for i in queryset:
list.append(i['car_model'])
if not list:
list.append('No Cars Found')
data = {
'list': list,
}
return JsonResponse(data)
I need No Cars Found to be displayed in the dropdown when no matching cars found in DB.