开发者

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

I've been having 开发者_开发技巧issues with django's template system tag 'ifchanged'. I can't quite pin down it's behavior.

Basically, if I had a setup like this:

{% for c in list %}
    {% ifchanged c.group %}
        group has changed!
    {% else %}
        {% ifchanged c.active %}
            item is active!
        {% endifchanged %}
    {% endifchanged %}
{% endfor %}

If c[0] had its c.group changed, then c[1] has not, but c.active has changed, sometimes "item is active!" will not show up.

This leads me to believe that ifchanged only checks on the last value evaluated by ifchanged and not the last value in the forloop. Is this correct?

If so, I may have to jump through some hoops to make this work.

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜