I want to pass an Array to a template and afterwards use it via JavaScript.
In my views.py I have:
arry1 = ['Str',500,20] return render_to_response('test.html', {'array1': arry1})
And in my template:
var array1 = {{ array1 }};
but when I visit the website it outputs:
var array1 = ['Str',500,20];
What do I have to change?
Adding JavaScript to Our Django TemplateWe can add JavaScript to our template using an inline <script> tag or an external JavaScript file. Let's create a app. js file, put it in a js folder that you need to create in the static folder of your application.
To load JavaScript file, just add the following line of code in index. html file. Run the server by using python manage.py runserver command. After that access the template by localhost:8000/index URL, and it will produce the following output to the browser.
POST form (your current approach) This answer is perfect and I learned a great deal!
Try using {{ array1|safe }}
and see if that makes any difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With