我试图制作ajax函数将列表发布到django视图,但我无法正确使用。
我的javascript函数:
function order_timers(data) {
console.log(data)
#Output: timer[]=38&timer[]=39&timer[]=25
$.ajax({
url: '{% url 'reorder_timers' %}',
type: "POST",
data: data,
success: function (response) {
console.log(response)
#Output: 383925
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError)
}
});
};
我的django观点:
@csrf_exempt
def reorder_timers(request):
timerlist = request.POST.getlist('timer[]') #think this is the line that does not work
i = 1
for item in timerlist:
timer = Timer.objects.get(pk=item)
timer.index = i
timer.save()
i = i + 1
return HttpResponse(timerlist)
任何人都知道我做错了什么?