[Answered ]-Does django's ifchanged compare to the last value during the loop or the last value when ifchanged was last evaluated?

2👍

Well, after spending hours tinkering and (poorly) looking through django code, I finally came up with something that works. I think it’s safe to say that ifchanged works with what it has already evaluated, and not what the last value in the loop was.

Here’s an example of code that works for the above situation:

{% for c in list %}
    {% ifchanged c.group %}
        group has changed!
    {% endifchanged %}

    {% ifchanged c.active %}
        {% ifchanged c.group %}
        {% else %}
            item is active!
        {% endifchanged %}
    {% endifchanged %}

{% endfor %}

This way, c.active gets evaluated every time and ‘item is active!’ displays as it should. This could end up in a ‘gotcha’ at some point, depending on if the ‘c.group’ evals work between both ifchanged tests. I’m not sure, but it is working for me so far.

Hope this helps anyone else that ends up in this situation.

Leave a comment