python - 通过 $http.post 以 Angular 将数组发送到 django View

标签 python django angularjs

我想将此请求发送到 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/

相关文章:

javascript - 尝试为每一行编写测试用例

python - 如果另一个失败,pytest 可以运行测试吗?

python - Django:在数据库中不存在的两个日期范围之间进行分组

javascript - 如何在Firebase中制作特定的架构?

sql - Django 中的 CharField 和 TextField 有什么区别?

Django 仅预取相关模型的最新对象

angularjs - 使用 Angular 模块和 Node 模板以获得更好的重用能力

python - ImageEditor 选择图像的一部分以在 Plone 中添加注释

python - atom : hydrogen installed, 但代码未运行(在 macOS 上)

python - 如何使用 Python 脚本中的 Cocoa API 在 macOS 中最小化/最大化窗口?