I am trying to use Django for loop in JavaScript but I am getting a syntax error
<script>
var config = {
type: 'pie',
data: {
datasets: [{
data: [1780000, 1630000],
backgroundColor: [
'#ff0000', '#0000ff', '#ff0080', '#73ffff', '#5c26ff', '#002db3', '#ffff26', '#4cff4c', '#ff00ff'
],
label: 'Population'
}],
labels: [{% for label in labels %} {{ label }}, {% endfor %}]
},
options: {
responsive: true
}
};
</script>
this part is what i a have a problem with to be exact
{% for label in labels %} {{ label }}, {% endfor %}
just typing
{% for %}
gives me a syntax error I looked at similar stack overflow questions and they just use the for loop normally without any problems so why am I getting a syntax error. I even tried it in an an empty Js file and still get a syntax error. Am I forgetting to import something ? It works just fine the HTML file Help is much appreciated I have been stuck for hours now :)
CodePudding user response:
You can't directly use Django template tags in your JavaScript code. You can use them in your template files.
Please see this question to understand how to access them from JavaScript code.