results.html
Django 웹 사이트의 자습서를 따라 Django 웹 사이트의 자습서 에서 계단식 드롭 다운 목록을 만들려고했습니다 .
Django 태그 {{ }}
와 {% %}
javascript 함수를 사용할 때 구문 오류가 발생했습니다 . 내가 사용한 IDE는 Komodo Edit이며이 줄 {% for item in question.choice_set.all %}
을 빨간색으로 강조 표시 하고 오류를 표시했습니다. Javascript : SyntaxError : expected expression, got '%'.
이 문제를 어떻게 해결할 수 있는지 묻고 싶습니다.
대단히 감사합니다! 아래는 내 html 스크립트입니다.
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>
임베디드 js에서 뷰의 데이터를 사용하려면 뷰 에서이 데이터를 수동으로 직렬화 해야합니다 (예 : 프로젝트에서 구현 한 방법).
CBV의 get_context_data : (django 1.6 python 2.7)
import json
from django.utils.safestring import mark_safe
# ...
def get_context_data(self, **kwargs):
context = super(LargeMapView, self).get_context_data(**kwargs)
human_values = Human.objects.values(
'pk', 'fio',
'lat_deg', 'lat_min', 'lat_sec',
'lon_deg', 'lon_min', 'lon_sec',
)
context['human_data'] = mark_safe(json.dumps(list(human_values), ensure_ascii=False))
return context
템플릿의 일부 :
<script>window.jQuery || document.write('<script src="{% static 'js/jquery-1.11.1.min.js' %}"><\/script>')</script>
<script type="text/javascript">
var humanList = {{ human_data }};
$.each(humanList, function (index, human) {
var coords = getCoords(
human['lat_deg'], human['lat_min'], human['lat_sec'],
human['lon_deg'], human['lon_min'], human['lon_sec']
);
});
</script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다