[Answered ]-Can variables with lookups be used in Django {% ifequal %} expressions?

2👍

There’s a difference between an object’s string representation, and what it actually is. It seems likely that your state property returns an object whose unicode representation is InProgress, but that doesn’t make it actually equal to "InProgress". This would be true for example if state is a ForeignKey to another model – in which case you could just add another level to the lookup to get to the actual field that returns that status.

Edit after update I think you’ve coded yourself into a corner, unfortunately. Since there’s no actual instance attribute that contains the state as a string, there’s no way to get it via string comparison.

A couple of possibilities spring to mind. One is to pass all possible State subclasses to the template context (perhaps via a context processor), after which you’ll be able to compare your state with the actual objects. This is pretty horrible.

An alternative would be to add a function, either on the State class or possibly on the model, which gets the state as a string. This could be as simple as the existing __unicode__ method (you can’t actually use that, because the template language forbids accessing attributes that begin with underscores). Then you can do ifequal foo.bar.0.state.as_string "InProgress" or whatever, and that will call the as_string() method and your comparison will succeed.

0👍

Try to use IF:

{% if thing.syncedthing_set.all.0.state == "InProgress" %}

Leave a comment