我有一个Django模板,它遍历记录,我想根据每条记录元素的平均值计算来修改div标签。我尝试以下面的方式来做,但它不起作用。知道如何使用JavaScript动态修改Django for循环中的标签吗?
{% for resource in result_basic %}
<script type="text/javascript">
var loc_rating = "{{resource.loc_rating}}";
var fac_rating = "{{resource.fac_rating}}";
var tran_rating ="{{resource.tran_rating}}";
var property_avg = (Number(loc_rating)+Number(fac_rating)+Number(tran_rating))/3;
document.getElementsByClassName('pratings-{{forloop.counter}}').innerHTML=property_avg;
</script>
<p>Property Ratings: <div class="pratings-{{forloop.counter}}"></div></p>
{% endfor %}