CSofiane
CSofiane

Reputation: 35

If in Html calling a model

I have this model:

class Canva(models.Model):

    name = models.CharField(null=True, blank=True, max_length=255)
    site = models.CharField(null=True, blank=True, max_length=255)
    month = models.CharField(null=True, blank=True, max_length=255)
    year = models.IntegerField(null=True, blank=True)
    
    def __str__(self):
        return self.name

in html page: when I write {{ canva.month }}, it shows me the value JUNE

but when I write:

{% if JUIN == canva.month %}

hello

{% else %}

goodbye

{% endif %}

it always show me goodbye.

Upvotes: 0

Views: 42

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476669

You should compare it with a string, not with identifier, so:

{% if canva.month == 'JUNE' %}
  hello
{% else %}
  goodbye
{% endif %}

I would however strongly advise to use a DateTimeField, and not store dates as a collection of strings. It is easy to save for example 'JUIN' instead of 'JUNE', and it makes comparing two dates (close to) impossible.

Upvotes: 3

Related Questions