VSCodeBeautify
VSCodeBeautify copied to clipboard
VSCodeBeautify has deleted Django tags within a <scritp> section, in a Django html template file.
in a Django html template file, there is a section of
<script>
var ctx = document.getElementById("chart_revenue").getContext("2d");
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: {{ data_regions | safe }},
datasets: [
{
label: "营收",
data: {{ data_revenue | safe }},
backgroundColor: "green",
borderWidth: 1,
}
],
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
</script>
After formatting, the result is:
<script>
var ctx = document.getElementById("chart_revenue").getContext("2d");
var myChart = new Chart(ctx, {
type: "bar",
data: {
labels: {
{
data_regions | safe
}
},
datasets: [{
label: "营收",
data: {
{
data_revenue | safe
}
},
backgroundColor: "green",
borderWidth: 1,
}],
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
},
}, ],
},
},
});
</script>
which is wrong.
I have the same issue - is there a way one can have Beautify skip formatting inbetween tags?
EDIT - BETTER WORKAROUND TO SEPARATE HTML and JAVASCRIPT I've learned of: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#json-script which is the new recommended way of passing data to javascript.
It does turn it to two lines of code however you can have
{{ value|json_script:"hello-data" }}
in your html
then just load a javascript file (below the above line) with the following embedded in it:
const value = JSON.parse(document.getElementById('hello-data').textContent);
@ihelmer07 It is possible to skip formatting inbetween ? How?