Django FOR LOOP in JavaScript [英] Django FOR LOOP in JavaScript
问题描述
为此,我正在使用Django。
在我的views.py中,我有一个名为points的变量,我将一些坐标写在列表中,作为一个字符串。
例如,
points =('-15.4566620,28.07163320','15.7566620,29.07163320',... 。)
然后,在Google地图脚本中,我有:
var flightPlanCoordinates = [
new google.maps.LatLng(-15.4566620,28.07163320),
new google.maps.LatLng(-15.7566620,29.07163320),
];
所以当我显示我的页面时,我看到这些点之间的一行。
我想要,而不是这样:
var flightPlanCoordinates = [
{%点数%}
new google.maps.LatLng({{point}}),
{%endfor%}
];
但这不行。
我做错了什么,应该怎么做?
非常感谢你。 >
目的是让模板渲染路径数组与硬编码相同。您应该检查渲染网页的源代码,以确保。
最好删除该逗号的逗号,即使它可以使用它。您可以使用 forloop.last
在最后一点省略它。
我正在追随一种风格在民意调查教程。确保视图将点
变量发送到模板:
urls.py
urlpatterns
包含 url(r'^ map / $' views.map'),
views.py
$ map $($ 0,0','10,0','10,10','0,10')
return render_to_response('polls / map.html',{'points':points})
模板map.html
...
var mypolyline = new google.maps 。polyline({
map:map,
path:[
{%in points in points%)
new google.maps.LatLng({{point}}){%如果不是forloop.last%},{%endif%}
{%endfor%}
]
})
First of all, I am a newbie at programming. I am trying to make a web page where you have a Google Map embedded, and in this Map you can see a colored line that sets a path. For this, I am using Django. In my views.py I have a variable called points, where I have some coordinates written in a list, as a string. For example,
points = ('-15.4566620,28.07163320','15.7566620,29.07163320',....)
Then, in the google maps script, I have:
var flightPlanCoordinates = [
new google.maps.LatLng(-15.4566620,28.07163320),
new google.maps.LatLng(-15.7566620,29.07163320),
];
So when I display my page, I see a line between those points.
I would like to have, instead of that, something like:
var flightPlanCoordinates = [
{% for point in points %}
new google.maps.LatLng({{point}}),
{% endfor %}
];
But this doesn't work.
What am I doing wrong, and how should it be?
Thank you very much.
The objective is to get the template to render the path array the same as if it were hardcoded. You should check the rendered webpage's source code to be sure.
It's best to remove that trailing comma, even if it does work with it. You can use the forloop.last
to omit it in the last point.
I'm following a style as in the polls tutorial. Make sure the view is sending the points
variable to the template:
urls.py
urlpatterns
contains url(r'^map/$', 'polls.views.map'),
views.py
def map(request):
points = ('0,0', '10,0', '10,10', '0,10')
return render_to_response('polls/map.html', { 'points': points })
template map.html
...
var mypolyline = new google.maps.Polyline({
map: map,
path: [
{% for point in points %}
new google.maps.LatLng({{ point }}) {% if not forloop.last %},{% endif %}
{% endfor %}
]
})
这篇关于Django FOR LOOP in JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!