Если вы передаете данные, используя Content-Type: application/json
, вы можете получить доступ к 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>