0

There is a Django template in my web app which has a JavaScript section in it.

chart.html

{% block footer_javascript_section %}<script type="text/javascript" >
g = new Dygraph(

// containing div
document.getElementById("graphdiv"),

// CSV or path to a CSV file.
"{{ biogas_plant_str }}",

{width: 1000,
height: 400,
labels: {{biogas_plant_list}} }  /*  <---- THIS list */ 
);
</script> 
{% endblock footer_javascript_section %}

I've passed in a Python list in the context of the chart view function.The list is referred by biogas_plant_list.

The issue

I need to embed the Python list as it is, without splitting or iterating over it. Other questions similar to this one, required the list to be handled in some way. A list has to be output after substitution. ['s1','s2'] However template substitutes the HTML character entities instead of the actual decoded characters. How to turn it off?

[&#39;&quot;25.7\\n&quot;&#39;, &#39;&quot;26.3\\n&quot;&#39; ]

2
  • Can we simmply not join the list and pass it as a String variable to the template. Commented Apr 1, 2018 at 11:51
  • I suppose it could be done.Might have to split it again when passing it the JS constructor. Commented Apr 1, 2018 at 11:58

1 Answer 1

1

From the Django docs, Django templates auto-escape quotes characters and such by default.

autoescape

Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.

 {% autoescape off %}
labels: {{biogas_plant_list}} }
{% endautoescape %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.