我在 POST 中将这些数据传递给 django 视图:
var jobj = [];
$('.some_class').each(function(){
var ro = $('#some_id').html(); //html() output is number
var nr = $('#something').html(); //html() output is number
var item = {};
item["prog"] = ro;
item["act"] = nr;
jobj.push(item);
});
arr = JSON.stringify(jobj);
ajax_req = $.ajax({
url: '../some_url/',
type: "POST",
data: {req_arr : arr},
});
数据如下所示:
req_arr = [{"prog" : "1", "act" : "0"}, {"prog" : "2", "act" : "0"}, (...)]
现在在视图中,我想遍历传递的数据,访问它,然后在 sql 中对其进行处理:
def some_url(request):
if request.method == 'POST':
arr = request.POST['req_arr']
for data in arr:
prog = data['prog']
act = data['act']
(...) some sql stuff like SomeModel.objects.get(name=prog) (...)
return HttpResponse(status=200)
我不知道如何访问单个值和键,就像它是 python 字典一样。