3

I have some code like this.

def foobar():
    foo = [" 1", " 2", " 3"]
    context['json'] = json.dumps(foo)
    print context['json']
    return render_to_string('template', context)

I am getting what I think is correct printed to the terminal...

[" 1", " 2", " 3"]

but then in my javascript console I get the error (index):59 Uncaught SyntaxError: Unexpected token ;

and when I go to where it is I see this...

[\u0022 1\u0022, \u0022 2\u0022, \u0022 3\u0022]

So I can see it's turning to unicode somewhere and then not being converted back but IDK what to do about it.

In my template (in <script> tags) I am doing this:

var data = {
    labels: {{ labels|escapejs }}
    datasets: []
}

1 Answer 1

3

Since you are trying to pass an a json representation of an array to your javascript through the django template, escapejs shouldn't be used! It should just be

labels: {{ labels }}

note that in your view you are using the variable name json while in your template you are using labels. I assume this is a typo

If you are having trouble with quotes,

{% autoescape off %}{{ labels }}{% endautoescape %}
Sign up to request clarification or add additional context in comments.

2 Comments

yes, typo...If I take away the escapejs I see "&quot; fish&quot"instead of the unicode code
Glad to have helped, i will edit my comment into the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.