Reputation: 89
I have an analytic dashboard with Django as a backend. After analyzing data in Python I'm sending a dictionary with name and salience.
var things_mentioned = {{entity.name}};
var things_salience = {{entity.salience}};
which produces an error:
SyntaxError: expected expression, got '&'
Below code does not produce any error and works perfectly:
var things_mentioned = ['noodles', 'food', 'Chinese', 'flavor', 'umami flavor'];
var things_salience = {{entity.salience}};
when I try to view {{entity.name}}
value in plain HTML page it exactly displays this
['noodles', 'food', 'Chinese', 'flavor', 'umami flavor'].
Also, there is not a single '&' in my entire program.
Upvotes: 2
Views: 1091
Reputation: 476584
It will try to HTML-escape the string. You can use JavaScript escaping. The safest way is probably to use the |json_script
template filter [Django-do]. You can thus use this as:
{{ entity.name|json_script:"things_mentioned" }}
var things_mentioned = JSON.parse(document.getElementById('things_mentioned').textContent);
Upvotes: 2