Jinja2 for循环在列表不工作,但访问单个元素的工作 [英] Jinja2 for loop in javascript on a list not working but accessing individual elements works
本文介绍了Jinja2 for循环在列表不工作,但访问单个元素的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Python代码
resultroute ['checkpointlist'] = CheckPoint。 query.filter_by(route_id = route.code)
return render_template('routes / edit.html',route = resultroute)
Javascript中的edit.html
function addExistingMarkers(){
//单独访问元素
var name0 ='{{route.checkpointlist [0] .name}}';
var lat0 = {{route.checkpointlist [0] .latitude}};
var long0 = {{route.checkpointlist [0] .longitude}};
var marker = new google.maps.Marker({
position:new google.maps.LatLng({{route.checkpointlist [0] .latitude}},{{route.checkpointlist [0]。经度}}),
map:map,
title:'{{route.checkpointlist [0] .name}}'
});
//尝试迭代列表
{%for route.checkpointlist%中的检查点}
var lat = checkpoint.latitude;
var long = checkpoint.longitude;
var cpname = checkpoint.name;
var location = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({
map:map,
draggable:true,
title:cpname,
animation:google .maps.Animation.DROP,
position:location,
});
{%end for%}
}
这是来自[0]元素的单独访问。但不知何故for循环不工作。
解决方案
{%for checkpoint in route.checkpointlist%}
var lat = {{checkpoint.latitude}};
var long = {{checkpoint.longitude}};
var cpname = {{checkpoint.name}};
var location = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({
map:map,
draggable:true,
title:cpname,
animation:google .maps.Animation.DROP,
position:location,
});
{%end for%}
在引用变量时需要包含双括号Jinja模板。
I am working on a flask + jinja2 website which involves plotting some stored markers on a map.
Python code
resultroute['checkpointlist'] = CheckPoint.query.filter_by(route_id=route.code)
return render_template('routes/edit.html',route=resultroute)
Javascript in edit.html
function addExistingMarkers() {
//Individual access to elements
var name0 = '{{route.checkpointlist[0].name}}';
var lat0 = {{route.checkpointlist[0].latitude}};
var long0 = {{route.checkpointlist[0].longitude}};
var marker = new google.maps.Marker({
position: new google.maps.LatLng({{ route.checkpointlist[0].latitude }}, {{ route.checkpointlist[0].longitude }}),
map: map,
title: '{{ route.checkpointlist[0].name }}'
});
//Trying to iterate over the list
{% for checkpoint in route.checkpointlist %}
var lat = checkpoint.latitude;
var long = checkpoint.longitude;
var cpname = checkpoint.name;
var location = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
draggable:true,
title:cpname,
animation: google.maps.Animation.DROP,
position: location,
});
{% end for %}
}
Only one marker is getting placed which is from the individual access of [0] element. But somehow the for loop is not working.
解决方案
{% for checkpoint in route.checkpointlist %}
var lat = {{checkpoint.latitude}};
var long = {{checkpoint.longitude}};
var cpname = {{checkpoint.name}};
var location = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
map: map,
draggable: true,
title: cpname,
animation: google.maps.Animation.DROP,
position: location,
});
{% end for %}
You need to include the double braces when referencing variable in a Jinja template.
这篇关于Jinja2 for循环在列表不工作,但访问单个元素的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文