Код Django:
class IndexView(View):
def get(self, request):
model = TireModel.objects.all()
text = request.GET.get("button_text")
if request.is_ajax():
t = time()
return JsonResponse({"seconds": t}, status=200)
context = {
"tires": model
}
return render(request, "index.html", context)
def post(self, request):
fields = ["en_mm", "height", "diametr", "season", "cartype"]
qs = TireModel.objects.all().values_list()
# qs1 = serialize_tires(qs)
# print(qs1)
for field in fields:
if request.POST.get(field) != "" and request.POST.get(field) != "All":
qs = qs.filter(**{field: request.POST.get(field)})
return JsonResponse({"data": qs})
Код в AJAX:
$(document).on("submit", function(e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '',
data: {
csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
},
success: function(response) {
console.log(response)
}
})
})
Как сделать отправку данных из модели Django в AJAX? Но после цикла for переменная qs выводит пустой QuerySet ()
for field in fields:
if request.POST.get(field) != "" and request.POST.get(field) != "All":
qs = qs.filter(**{field: request.POST.get(field)})
print(qs)