Django FOR LOOP in JavaScript [英] Django FOR LOOP in JavaScript

查看:171
本文介绍了Django FOR LOOP in JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是编程的新手。我正在尝试制作一个网页,您可以在其中嵌入Google地图,而在此地图中,您可以看到设置路径的彩色线。
为此,我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆