[Answered ]-Using Django Choices in Template Logic

2👍

✅

I would write a property method in the model

class Day(models.Model):

    [...]

    @property
    def is_rainy(self):
        return self.weather == self.WEATHER.rainy

And in my template I would check:

{% if monday.is_rainy %}
    It rained monday
{% endif %}

0👍

I (foolishly) had already implemented this in my code and had forgotten about it! One can use the instance to refer to the choice possibilities, per my example:

{% if monday.weather == monday.WEATHER.rainy %}
[...]

I would delete the question, but the fact remains it is not explained very clearly in the docs and does not appear to be a duplicate of anything I was able to find on stackoverflow.

Leave a comment