views:

16

answers:

1

Hi! I'm new to django and can't find a way to get this to work in django templates. The idea is to check if previous items first letter is equal with current ones, like so:

{% for item in items %}
    {% ifequal item.name[0] previous_item.name[0] %}
        {{ item.name[0] }}
    {% endifequal %}
    {{ item.name }}<br />
{% endforeach %}

Maybe i'm trying to do this in wrong way and somebody can point me in right direction.

+1  A: 

Use the {% ifchanged %} tag.

{% for item in items %}
    {% ifchanged item.name.0 %}
        {{ item.name.0 }}
    {% endifchanged %}
{% endfor %}

Also remember you have to always use dot syntax - brackets are not valid template syntax.

Daniel Roseman
Typo at the end
kovshenin
@kovshenin thanks, fixed
Daniel Roseman
Thanks a lot, this did the job perfectly!
ronalds