I am trying to figure out how to deal with POSTed json objects in Django. I'm POSTing a json object to the server and want to use it like a python dictionary.
here is my js:
$.post(
"/projects/vote/",
obj,
function(data) {
//alert("Data Loaded: " + data);
alert(data["msg"]);
});
What I am returning (end of django view):
return HttpResponse(json.dumps(foo), mimetype="application/json")
where
foo = {"msg": str(postdata)}
In other words, I'm POSTing a json object to the server, and alerting the string of the python object I get on the server so I can see what's going on.
If my obj is:
var obj = {
'bulk': false,
'data': {
'chosen': '14',
'proj1': '15',
'proj2': '14',
'proj3': '16',
'more': false,
},
'data2': [
{
'a': 'apple'
},
{
'b': 'banana'
},
],
}
I get this in return:
<QueryDict: {u'data[proj3]': [u'16'], u'data[proj2]': [u'14'], u'data[chosen]': [u'14'], u'data[proj1]': [u'15'], u'bulk': [u'false'], u'data2[0][a]': [u'apple'], u'data[more]': [u'false'], u'data2[1][b]': [u'banana']}>
How come the structure of the json obj and python dict don't align? And how do I get the structure to be the same? e.g. instead of data2[0][a], I would get data2 as the key to another dictionary
How I'm getting postdata:
# django version 1.4
postdata = request.POST.copy()