Django: passing JSON from view to template

If a frontend library needs a to parse JSON, you can use the json library to convert a python dict to a JSON valid string. Use the escapejs filter

import json

def foo(request):
    json_string = json.dumps(<time_series>)
    render(request, "foo.html", {'time_series_json_string': json_string})

    var jsonObject = JSON.parse('{{ time_series_json_string | escapejs }}');

Leave a Comment