2014-07-07 41 views
0

上下文名單在我看來,我通過上下文的模板,其中包含的(X,Y)的二維座標列表:索引模板

_points = [[100, 100], [200, 200], [300, 200]] 
context = {'points': _points} 
return render(request, 'mytemplate.html', context) 

在我的模板,我想繪製這些點之間的線路:

var c = document.getElementById("graph"); // "graph" is a canvas element 
var ctx = c.getContext("2d"); 
ctx.beginPath(); 
// Move to the first point 
ctx.moveTo({{ points[0][0] }}, {{ points[0][1] }}); 
// Loop through all points and draw connecting line 
{% for point in points %} 
    ctx.lineTo({{ point[0] }}, {{ point[1] }}); 
{% endfor %} 
ctx.stroke(); 

但是,我上線的錯誤:

ctx.lineTo({{ point[0] }}, {{ point[1] }}); 

Could not parse the remainder: '[0]' from 'point[0]'

什麼是正確的方法是從上下文索引列表變量?

回答

3

你可以解開這個名單原地的,就像這樣:

{% for x, y in points %} 
    ctx.lineTo({{ x }}, {{ y }}); 
{% endfor %} 

(見Django documentation for for

如果你想幫助他們走出「速記」,你應該能夠編寫{{ point.0 }} - 雖然我同意奇怪的是單個索引無法正常工作。