Dictionary as string in django template

So, I figured out that we can use script tags inside for loop. I’ve done parsing inside that script tags and printed value using innerHTML.

{%for eachObject in dict%}
   {{eachObject.key1}}
   <span id='key'></span>
   <script>
     var key3="{{eachObject.key3}}"
     // console key3 and check if it has got &quot instead of '. It happend in my case. If so, add the below line:
     key3 = JSON.parse(key3.replace(/&quot;/g,'"'));
     // if your key3 is without &quot, then do JSON.parse(key3) in above line
     document.getElementById("key").innerHTML=key3.name
   </script>
{%endfor%}

Make a note to keep span tags above script tags

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top