[Django]-Unable to use dict sent from Django to template

4👍

You need JSON, not dict. Use json.dumps function:

import json

mydict = {
    "ABC": {
        "target_weight": 1,
        "target_ra_weight": 2
    },
    "XYZ": {
        "target_weight": 3,
        "target_ra_weight": 4
    },
    "JKL": {
        "target_weight": 5,
        "target_ra_weight": 6
    }
}
return render(request, 'template.html', {'mydict': json.dumps(mydict)})

and in template:

var mydict = JSON.parse('{{ mydict|safe }}');

Leave a comment