<script>
//Handle the callback on success
function stateChange(){
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
//request is successful. So retrieve the values in the response
var display_language = xmlHttp.responseText.split(';');
alert("response: " + display_language);
var newHtml = '{% for chunk in video.chunks.all %} {% for c in chunk.chunklets.all%} <tr> <td>{{c.start}}</td> <td> {% spaceless %} {%for item in c.contents.all%}{%if item.lang == display_language %}{{item.text.strip}}{%endif%}{%endfor%} {% endspaceless %} </td> <td>{{c.finish}}</td> </tr> {%endfor%} {%endfor%}';
$('#xxx').html(newHtml);
}
}
</script>
If I use the above code, the display_language value is null. But alert can print the value of display_language. Just the display_language can not be passed into newHtml.
If it is changed to
var newHtml = '{% for chunk in video.chunks.all %} {% for c in chunk.chunklets.all%} <tr> <td>{{c.start}}</td> <td> {% spaceless %} {%for item in c.contents.all%}{%if item.lang ==';
newHtml += display_language +'%}{{item.text.strip}}{%endif%}{%endfor%} {% endspaceless %} </td> <td>{{c.finish}}</td> </tr> {%endfor%} {%endfor%}';
The error is that “”Invalid block tag: ‘endif’, expected ’empty’ or ‘endfor’
You’re trying to mix Javascript and Django templates too much. Remember that all of the Django is run first, and then all of the javascript will be run. So because the
display_languagevalue is created in Javascript, you can’t use it in your Django template. I would recommend just passing all of your data into Javascript, and then constructing your newHtml value in pure javascript.