Django: want to use loop.counter to assign letter for Google Maps marker

Django: want to use loop.counter to assign letter for Google Maps marker

猫性小仙女 发布于 2021-11-26 字数 1078 浏览 923 回复 3 原文

I've got a variable number of items, somewhere between 0 and 20.

I'd like to list these with Google Static Maps, showing a little "a" for the first one, a "b" for the second one and so on.

I'm a newbie using Google App Engine so I'm constrained to 0.96 (unless I use various patches, which I don't want to do. Because I'm a newbie.)

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

is working fine to provide a list of red markers.

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

gets me 0-9 on the map.

For now, I've cut the result set down to 10. I'd like to go back to 20. Is there a way of using the loopcounter and slice (as in {{ alpha_list|slice:":loop_counter"}} ? I struggled with various incantations, trying {{ }} around loop_counter and without and couldn't get it to work.

Thanks!

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖哒 2022-06-07 3 楼

create a template tag using method provided here

How do I iterate through a string in Python?

放飞的风筝 2022-06-07 2 楼

If you want do it entirely within the template, you can use the cycle tag.

Something like the following, with ... expanded:

{% cycle 'a' 'b' ... 'z' as alphabet %}
&markers={% for item in results %}{{item.latitude}},{{item.longitude}},{% cycle alphabet %}{% if not forloop.last %}|{% endif %}{% endfor %}
云雾 2022-06-07 1 楼

Easiest would be to write a template tag. There's a good tut, but the code would basically be:

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