我想将此请求发送到 django View :
$http({
method: "post",
url: "/enterprises/vouchers/_send",
data: {
group_id: group_id,
group_student_ids: [1, 2, 3, 4]
}
}).success(function (response) {
console.log("emails are sent. please check");
}).error(function () {
console.log("failed")
});
在 View 中我这样分配它们:
group_student_ids = request.POST['group_student_ids']
group_id = request.POST['group_id']
“group_id”按预期分配,但 django 为数组对象 (group_student_ids) 抛出 MultiValueDictKeyError
如何通过 post 请求发送数组?
最佳答案
这应该有效:
首先,更改您的 data
到:
data: {
'group_id': group_id,
'group_student_ids[]': [1, 2, 3, 4]
}
在你看来:
group_student_ids = request.POST.getlist('group_student_ids[]')
编辑:
刚刚在我的应用中做了一些测试;如果我打印 request.POST
, 我会得到
<QueryDict: {'group_student_ids[]': ['1', '2', '3', '4']}>
和type(group_student_ids)
会给 <class 'list'>
关于python - 通过 $http.post 以 Angular 将数组发送到 django View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520802/