I want to send this request to a 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")
});
In the view I assign them like this:
group_student_ids = request.POST['group_student_ids']
group_id = request.POST['group_id']
"group_id" is assigned as expected but django is throwing MultiValueDictKeyError for the aray object(group_student_ids)
How do I send an array via a post request?
This should work:
First, change your data to:
data: {
'group_id': group_id,
'group_student_ids[]': [1, 2, 3, 4]
}
and in your view:
group_student_ids = request.POST.getlist('group_student_ids[]')
Edit:
Just did some tests in my app; if I print request.POST, I'll get
<QueryDict: {'group_student_ids[]': ['1', '2', '3', '4']}>
And type(group_student_ids) would give <class 'list'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With