Django json请求后解析

Django json请求后解析,django,post,web,request,Django,Post,Web,Request,我的客户端正在将此json作为post传递到django服务器: data={ 'supplier': supplier_name, 'date': date, 'payment':payment, 'materials':[{"name":name,"qtd":qtd,"price":price}, {"name":name,"qtd":qtd,"price":price},

我的客户端正在将此json作为post传递到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}]
}
我正在使用推送来放置材料:

data['materials'].push({"name":name,"qtd":qtd,"price":price});
My django视图处理如下数据:

supplier=request.POST.get('supplier')
date=request.POST.get('date')
$.ajax({
    type:"POST",
    url:"{% url 'validate_purchase' %}",
    data: data,
    dataType: 'json',
    success: function(data){
    }
});
当我尝试这样做时,材料内容为“无”:

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

Ajax的发送方式如下:

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

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

例如:

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

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

例如:

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

作为json发送的数据必须是“stringify”,所以您需要执行“json.stringify(Data)”


作为json发送的数据必须是“stringify”,所以您需要执行“json.stringify(Data)”


重复项查看此答案这就是您的客户传递给django应用程序的内容-
{“name”:name,“qtd”:qtd,“price”:price}{“name”:name,“qtd”:qtd,“price”:price}
?看起来这不是一个有效的JSON,您在
材质
数组中遗漏了
}{
。@Chiefir我遗漏了”,“因为这只是一个示例,我使用的是数据['materials']。push({“name”:name,“qtd”:qtd,“price”:price})要放置数据,您应该显示JS中实际发送数据的部分。您是在
data
字段中使用JSON以表单编码的POST发送数据,还是直接以JSON POST的形式发送数据?使用jqueryduplicates中的ajax进行编辑查看此答案这正是您的客户端传递给django应用程序的内容-
{“name”:name,“qtd”:qtd,“price”:price}{“name”:name,“qtd”:qtd,“price”:price}
?看起来这不是一个有效的JSON,您在
材质
数组中遗漏了
之间的
{
。@Chiefir我遗漏了”,“因为这只是为了举例说明,我正在使用数据['materials']。push({“name”:name,“qtd”:qtd,“price”:price})要放置数据,您应该显示JS中实际发送数据的部分。您是在
data
字段中使用JSON以表单编码的POST形式发送数据,还是直接以JSON POST形式发送数据?JQUERY中的ajax编辑了此错误(提高JSONDecodeError(“期望值”),s,err.value)from None json.decoder.JSONDecodeError:Expecting value:line 1 column 1(char 0))从None json.decoder.JSONDecodeError:Expecting value:line 1 column 1(char 0))向我发出此错误(raise JSONDecodeError(“Expecting value”,s,err.value))您还可以将键值对作为数据传递,例如数据:
{“key”:“value”}
您还可以将键值对作为数据传递,例如数据:
{“key”:“value”}