A
A
Anton2018-09-26 13:35:39
JavaScript
Anton, 2018-09-26 13:35:39

How to pass a string, a mathematical expression, from Django to JS?

I pass data from Django to JavaScript using the Jinja template engine.
The code looks something like this:

a = ['200', '300']
    return render(request, 'OneApp/code1.html', {'test' : a})

var strr2 = []
strr2.push('100')
strr2.push('200')
{% for val in test %}
    strr2.push({{val}})
{% endfor %}
alert(strr2)

It seems like everything is fine, Java displays: "100,200,200,300"
But if you do this: I see that JS has calculated: "100,200,Infinity,300" Tell me how to correctly pass the string exactly as a string?
a = ['20/0', '300']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-09-26
@8toni8

Damn, I just asked a question and guessed it myself: (The quotes should have been put ...

{% for val in test %}
    strr2.push('{{val}}')
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question