Looking to pass a list of items to a JavaScript array, using Django version 2.2.3.
In my views.py:
my_list = ["one", "two"]
context = {
'my_list ': json.dumps(my_list),
}
return render(request, 'my_template.html', context)
In my_template.html:
<script>
var my_array = JSON.parse("{{ my_list|safe }}")
document.write(my_array )
</script>
Although many answers indicate that my code is correct, it does not display any data.
I have also tried:
<script>
var my_string = "{{my_list}}";
var my_array = my_string.split(',');
document.write(my_array)
</script>
When attempting to display the array in this way, I receive:
[" one "