String errors when passing to template

A bit new to django and having trouble passing context variables from my view to my html template. I have a dummy object created in my
my_obj = ['hello']
which is getting passed through the context.
Within my <body> tags, if I display {{ my_obj }}, this does what one would think and shows up as [‘hello’]

Within my <script> tags, however, this is throwing an error, and the console.log({{ my_obj}}); call I have in my .html is getting compiled as console.log([&#x27;hello&#x27;]); when I inspect it in the browser.

I’ve also tried using console.log({{ my_obj|escapejs }}); in my .html, which makes it compile as console.log([\u0027hello\u0027]); in the browser.

Is there any way to pass strings or other objects within the context so that they are able to be manipulated in JS within the <script> tags

I think you’ve got at least two options here.

First is the “safe” filter.

The other one that might help is json_script.


1 Like

The safe filter fixed it, thanks!