2009-04-27 31 views
1

我有一個可變數量的項目,在0至20Django的:要使用loop.counter分配信谷歌地圖標記

地方

我想與谷歌靜態地圖列出這些,第一個顯示一個「a」,第二個顯示「b」等等。

我使用谷歌應用程序引擎,所以我限制爲0.96(除非我用各種補丁,這是我不想做的事。因爲我是個新手。)新手

&markers={% for item in results %}{{item.latitude}},{{item.longitude}}{% if not forloop.last %}|{% endif %}{% endfor %} 

正在正常工作以提供紅色標記的列表。

&markers={% for item in results %}{{item.latitude}},{{item.longitude}},{{forloop.counter0}}{% if not forloop.last %}|{% endif %}{% endfor %} 

讓我在地圖上0-9。

現在,我已經將結果設置爲10.我想回到20.是否有一種使用loopcounter和slice的方法(如{{alpha_list | slice:「:loop_counter 「}}?我用各種咒語掙扎着,試圖{{}}各地loop_counter並沒有,不能讓它的工作。

謝謝!

回答

1

提供。

像下面,有...擴展:

{% cycle 'a' 'b' ... 'z' as alphabet %} 
&markers={% for item in results %}{{item.latitude}},{{item.longitude}},{% cycle alphabet %}{% if not forloop.last %}|{% endif %}{% endfor %} 
+0

我非常喜歡這個!謝謝。 – jdeibele 2009-05-15 19:58:56

2

最簡單的是寫一個模板標籤,有一個good tut ,但代碼基本上是:

def inttoalpha(n): 
    a = ord('A') 
    return chr(a+n) 
+0

感謝您的領先優勢。實現標籤看起來像我稍後會回頭看看。我現在要堅持10項。 – jdeibele 2009-05-05 17:58:24