1

I'm using Django for my webapp. I'm sending a JSON data to my views but I'm not able to access Nodes and edges by calling decoded_data['nodes'] and it gives me :

'NoneType' object is not subscriptable

error message.

Here is how I send json to to my view:

var a={
            nodes:   thisGraph.nodes,
            edges: saveEdges
        };

    //send data to server
    $(document).ready(function(){
    function change(){
        $.ajax({
        type:"POST",
        url: "/",
        data: {'data': JSON.stringify(a)},
        dataType: "json",  
        success: function(){
        console.log("Ajax worked");
        $('#message').text("Ajax worked");
        },
        headers:{'X-CSRFToken': csrftoken}
    });

here is my view:

data = request.POST.get("data")
json_encoded = json.dumps(data) 
decoded_data = json.loads(json_encoded)
logger.error(decoded_data['nodes'])

the decoded_data looks like this:

{"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":"
constraint","id":2,"title":"new Constraint","x":371,"y":95}],"edges":[{"source":
2,"target":0}]}

I appreciate your help

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hirad Roshandel
  • 2,175
  • 5
  • 40
  • 63
  • How does data look like? I tried running your code here with `data={"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":" constraint","id":2,"title":"new Constraint","x":371,"y":95}],"edges":[{"source": 2,"target":0}]}` and there are no errors – t.pimentel Aug 14 '14 at 22:08
  • Yes it works when I do what did but when I get it from POST.get("data") it doesn't work. request.POST looks like this: – Hirad Roshandel Aug 14 '14 at 22:16
  • Does it really change between ' and "? How did you print this? `print data`? – t.pimentel Aug 14 '14 at 22:21
  • Try this `myDict = dict(data.iterlists())` and see what happens (It seems like you have a QueryDict http://stackoverflow.com/questions/13349573/how-to-change-a-django-querydict-to-python-dict ) – t.pimentel Aug 14 '14 at 22:23
  • no it doesn't, I tried both. I used logger.error(request.POST) – Hirad Roshandel Aug 14 '14 at 22:23
  • 'NoneType' object has no attribute 'iterlists' – Hirad Roshandel Aug 14 '14 at 22:25
  • Ok, I think I got it now. Try `decoded_data = json.loads(data[0])` – t.pimentel Aug 14 '14 at 22:34
  • 'NoneType' object is not subscriptable. It seems i need json_encoded = json.dumps(data) others it keeps saying the JSON object must be str, not 'NoneType' – Hirad Roshandel Aug 14 '14 at 22:36
  • Then try `decoded_data = json.loads(decoded_data[0])` – t.pimentel Aug 14 '14 at 22:56
  • 1
    I think you meant `decoded_data = json.loads(json_encoded[0])` I did that it says `Expecting value: line 1 column 1 (char 0)` – Hirad Roshandel Aug 14 '14 at 23:01
  • No, realy try `data = request.POST.get("data") json_encoded = json.dumps(data) decoded_data = json.loads(json_encoded) decoded_data = json.loads(decoded_data[0])` And if it doesn't work print data for me. – t.pimentel Aug 14 '14 at 23:15
  • Same thing 'NoneType' object is not subscriptable – Hirad Roshandel Aug 14 '14 at 23:22
  • And can you print the value of `data`? – t.pimentel Aug 14 '14 at 23:26
  • This is what i get from `request.post.get("data")`: `{"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":" node","id":2,"title":"new Node","x":334,"y":60}],"edges":[{"source":2,"target":0 }]}`. Isn't there something wrong in my ajax call? – Hirad Roshandel Aug 14 '14 at 23:31
  • Can you print `data.__class__`? – t.pimentel Aug 14 '14 at 23:44
  • 'NoneType' is the type of None and should appear if a variable was not correctly set, or if it was set as `my_variable = None`. if data is a string equal to this: `{"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":" node","id":2,"title":"new Node","x":334,"y":60}],"edges":[{"source":2,"target":0 }]}` then `decoded_data = json.loads(data)` should work – t.pimentel Aug 15 '14 at 00:01
  • If that is not working by some reason try `data = request.POST.get("data") json_encoded = json.dumps(data) decoded_data = json.loads(json_encoded) decoded_data = json.loads(decoded_data)` or `request.POST.get("data") decoded_data = json.loads(str(data))` – t.pimentel Aug 15 '14 at 00:04
  • when i use json.loads(str(data)) it gives me `Expecting value: line 1 column 1 (char 0)` and the other one gives me `the JSON object must be str, not 'NoneType'` – Hirad Roshandel Aug 15 '14 at 01:04

3 Answers3

2

Change it to:

data = request.POST.get("data")
try:
    decoded_data = json.loads(data)
    nodes = decoded_data.get("nodes")
except:
    print("ERROR decoding")

request.POST.get("data") is a string. Just load it from there.

tom.alexander
  • 219
  • 1
  • 6
1

'NoneType' is the type of None (Equivalent to Null in python) and should only appear if a variable was not correctly set, or if it was set as my_variable = None.

If data is a string equal to this:

data = '{"nodes":[{"type":"node","title":"new concept","id":0,"x":658,"y":100},{"type":" node","id":2,"title":"new Node","x":334,"y":60}],"edges":[{"source":2,"target":0 }]}'

Then simply using the following code should work:

decoded_data = json.loads(data)

Check if your requests are really coming from the AJAX request or if data == None like this:

data = request.POST.get("data")
if data === None:
    return "Error: not correctly formed request"
else:
    decoded_data = json.loads(data)
    nodes = decoded_data["nodes"]
    edges = decoded_data["edges"]
t.pimentel
  • 1,465
  • 3
  • 17
  • 24
0

I found the problem! I was using this code in my "/" url and the first time this code was getting called data was Null so I had to check if the call was AJAX and use this code in that condition.

Hirad Roshandel
  • 2,175
  • 5
  • 40
  • 63