2

I am writing a django application, and in a loop,

    {% for item in list %}
    {{ forloop.counter0 }}
    {% endfor %}

this will printout the number in the loop starting from 0. But I want to printout alphabet starting from 'A', so the python way to do it is chr(forloop.counter0+65), but this is inside the template, any ideas? thanks.

bernardw
  • 1,370
  • 3
  • 15
  • 27

1 Answers1

9

You can write a simple custom template tag, for example a filter:

@register.filter(name='chr')
def chr_(value):
    return chr(value + 65)

Then load it in your template and you can do:

{{ forloop.counter0|chr }}
rczajka
  • 1,810
  • 14
  • 13