how to return a dictionary in python django and view it in javascript?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2
--
Chapters
00:00 How To Return A Dictionary In Python Django And View It In Javascript?
00:58 Accepted Answer Score 17
01:09 Answer 2 Score 10
01:27 Answer 3 Score 2
01:45 Answer 4 Score 1
01:56 Thank you
--
Full question
https://stackoverflow.com/questions/6467...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#javascript #python #django
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2
--
Chapters
00:00 How To Return A Dictionary In Python Django And View It In Javascript?
00:58 Accepted Answer Score 17
01:09 Answer 2 Score 10
01:27 Answer 3 Score 2
01:45 Answer 4 Score 1
01:56 Thank you
--
Full question
https://stackoverflow.com/questions/6467...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#javascript #python #django
#avk47
ACCEPTED ANSWER
Score 17
Very simply:
import json
data = {'val1' : 'this is x', 'val2' : True}
return HttpResponse( json.dumps( data ) )
ANSWER 2
Score 10
JSON is easiest way to transfer data(also you can use XML).
In python:
    import json
    data = {'val1': "this is x", 'val2': True}
    return HttpResponse(json.dumps(data))
In javascript:
    function (data) {
        data = JSON.parse(data);
        if (data["val2"]) {
            alert(data["val1"]);
        }
    }
ANSWER 3
Score 2
You can not directly use the python object you have to convert it into JSON string first Look into following documentation.
http://docs.python.org/library/json.html also http://www.json.org/
ANSWER 4
Score 1
Just specify the mimetype in HttpResponse
    return HttpResponse(
                        json.dumps({"status":False, "message":"Please enter a report name."}) ,
                        content_type="application/json"
                        )