通过$http发送一个数组。从角到django视图。

时间:2023-01-13 19:42:02

I want to send this request to a django view:

我想将此请求发送到django视图:

$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)

“group_id”按预期分配,但是django正在为aray对象(group_student_ids)抛出多值的错误代码。

How do I send an array via a post request?

如何通过post请求发送数组?

2 个解决方案

#1


1  

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'>

类型(group_student_ids)将提供 <类'list'> 。

#2


0  

Try this:

试试这个:

group_student_ids = request.POST.getlist('group_student_ids[]')

#1


1  

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'>

类型(group_student_ids)将提供 <类'list'> 。

#2


0  

Try this:

试试这个:

group_student_ids = request.POST.getlist('group_student_ids[]')