[Django]-Values get converted to UNICODE when sent by Python in Javascript

2👍

Looks like it’s getting HTML-escaped on output. What if you do this?:

var results = {{ myposts|safe }};

(Use with caution — you may want to perform some escaping depending where the data is coming from.)

👤JMM

1👍

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))
👤davids

Leave a comment