I followed the tutorials on Django website and tried to create cascading drop down lists at the results.html from the Django website's tutorials.
I have encountered a Syntax error when I use Django tags {{ }} and {% %} in javascript function. The IDE I used is Komodo Edit and it highlighted this line {% for item in question.choice_set.all %} as red and stated an error: Javascript: SyntaxError: expected expression, got '%'.
I would like to ask that how can I fix this?
Thank you very much! and as below is my html script.
results.html
<!doctype html>
<html>
<body>
<script type="text/javascript">
function change(chosen,updateList){
document.getElementById('text').value = chosen;
updateList.options.length=0;
{% for item in question.choice_set.all %}
if (item = chosen) {
updateList.options[updateList.options.length] = new Option({{item.votes}}, '')
}
{% endfor %}
}
</script>
<form name='form' action="{% url 'polls:results' quezstion.id %}", method='post'>
{% csrf_token %}
<h1>{{ question.question_text }}</h1>
<select name="Choice" onchange="change(document.form.Choice.options[document.form.Choice.selectedIndex].value, document.form.Votes)">
{% for item in question.choice_set.all %} //question used here is defined in views
<option value="{{item.id}}">{{ item.choice_text }}</option>
{% endfor %}
</select>
<select name="Votes">
<option></option>
</select>
</form>