【问题标题】:Django json post request parseDjango json 发布请求解析
【发布时间】:2018-10-19 21:07:48
【问题描述】:

我的客户正在将此 json 作为帖子传递给 django 服务器:

data={  'supplier': supplier_name,
        'date': date,
        'payment':payment,
        'materials':[{"name":name,"qtd":qtd,"price":price},
                    {"name":name,"qtd":qtd,"price":price},
                    {"name":name,"qtd":qtd,"price":price}]
}

我正在使用 push 来放材料:

data['materials'].push({"name":name,"qtd":qtd,"price":price});

我的 django 视图处理这样的数据:

supplier=request.POST.get('supplier')
date=request.POST.get('date')

当我尝试这样做时,材料内容为“无”:

materials=request.POST.get('materials')

如何在进一步的代码中使用列表?

Ajax 是这样发送的:

$.ajax({
    type:"POST",
    url:"{% url 'validate_purchase' %}",
    data: data,
    dataType: 'json',
    success: function(data){
    }
});

【问题讨论】:

  • 这正是您的客户传递给 django 应用程序的内容 - {"name":name,"qtd":qtd,"price":price} {"name":name,"qtd":qtd,"price":price}?看起来这不是有效的 JSON,您在 materials 数组中的 } { 之间错过了 ,
  • @Chiefir 我错过了 "," 因为这只是为了举例说明,我正在使用 data['materials'].push({"name":name,"qtd":qtd,"price" :price}) 放数据
  • 你应该展示你的JS中实际发送数据的部分。您是在data 字段中将其作为带有 JSON 的表单编码 POST 发送,还是直接作为 JSON POST 发送?
  • 已编辑,在 jquery 中使用 ajax

标签: django post web request


【解决方案1】:

如果您使用Content-Type: application/json 传递数据,您可以从request.body 访问json

例子:

(myblog)  ✘ ✝ ~/projects/myblog/base  up-sell±  curl --header "Content-Type: application/json" \
--request POST \
--data '{"supplier": "x", "date": "x", "materials": [{"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}], "payment": "x"}' \
http://localhost:8000/motor/upsell/set-upsell/ \
> -H "Content-Type: application/json"

views.py:

ipdb> import json
ipdb> json.loads(request.body)
{u'supplier': u'x', u'date': u'x', u'materials': [{u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}], u'payment': u'x'}

更新

ajax 调用示例

这里是ajax函数,

data = {"supplier": "x", "date": "x", "materials": [{"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}, {"price": "x", "qtd": "x", "name": "x"}], "payment": "x"}

$.ajax({
    type: 'POST',
    url: 'http://localhost:8000/motor/upsell/set-upsell/',
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: 'json'
});

Python 代码,

ipdb> import json
ipdb> request.body
'{"supplier":"x","date":"x","materials":[{"price":"x","qtd":"x","name":"x"},{"price":"x","qtd":"x","name":"x"},{"price":"x","qtd":"x","name":"x"}],"payment":"x"}'
ipdb> json.loads(request.body)
{u'supplier': u'x', u'date': u'x', u'materials': [{u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}, {u'price': u'x', u'qtd': u'x', u'name': u'x'}], u'payment': u'x'}
ipdb>

【讨论】:

  • 给我这个错误(从 None json.decoder.JSONDecodeError 引发 JSONDecodeError("Expecting value", s, err.value): Expecting value: line 1 column 1 (char 0))跨度>
【解决方案2】:

要作为json发送的数据必须是“stringified”,所以你需要做“JSON.stringify(data)”

$.ajax({
        type:"POST",
        url:"{% url 'validate_purchase' %}",
        data: JSON.stringify(data),
        dataType: "application/json; charset=UTF-8",
        success: function(data){
        }
    });

【讨论】:

  • 您也可以将键值对作为数据传递,例如数据:{"key": "value"}
猜你喜欢
  • 2014-03-22
  • 2019-10-27
  • 2014-09-17
  • 2020-10-14
  • 2021-12-20
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
相关资源
最近更新 更多