I have a small problem when my code is retrieved by javaScript from Python, i get unicode values to my script where my browser gives an error in the developer console.
The script inside my archive.html page –
<script>
var results = {{myposts}};
console.log(results);
</script>
My Python code –
def archive(request):
test = ["a","b","c"]
t = loader.get_template("archive.html")
c = Context({'myposts' : test})
return HttpResponse(t.render(c))
I tried c = Context({'myposts' : simplejson.dumps(test)}) , but it gave the problem.
My browsers give me and arror Uncaught SyntaxError: Unexpected token & and my console shows my array with unicode values ['a', 'b', 'c']
How do i make it look like – ["a","b","c"]
What do i change in my Python code or JavaScript
Thanks for the help in advance
Looks like it’s getting HTML-escaped on output. What if you do this?:
(Use with caution — you may want to perform some escaping depending where the data is coming from.)