Below is a Python dictionary that I am sending to a template:
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': mydict})
I'm having trouble assigning this data to a JavaScript variable and accessing its content. I've attempted the following in the JavaScript section of my template:
var mydict = {{ mydict }};
> Uncaught SyntaxError: unexpected token {
var mydict = {{ mydict|safe }};
> Uncaught SyntaxError: unexpected token {
var mydict = {{ mydict|escapejs }};
> Uncaught SyntaxError: unexpected token {
The code below allows me to create the variable, but attempting to access it ((e.g., console.log(mydict)) results in an error:
var mydict = JSON.parse('{{ mydict }}');
console.log(mydict);
> Uncaught SyntaxError: unexpected token { in JSON at position 1
// same thing happens when using |safe or |escapejs