If condition with field value don't work in Django template
In models I have:
class Book(models.Model):
status = models.ForeignKey(
'BookStatus',
verbose_name='Status',
on_delete=models.RESTRICT,
null=True,
blank=True,
)
and
class BookStatus(models.Model):
name = models.CharField(
max_length=50,
unique=True,
)
def __str__(self):
"""String for representing the Model object."""
return self.name
In views I have a normal Model DetailView:
class BookDetailView(LoginRequiredMixin, generic.DetailView):
login_url = '/accounts/login/'
model = Book
In the template for this detail view I want to display a button only, if book.status is not "vorhanden". I try with this:
{% if book.status != 'vorhanden' %}
...
{% endif %}
but this is also true, if the displayed book has status = vorhanden.
I tried the reverse:
{% if book.status == 'vorhanden' %}
...
{% endif %}
but this is not true, even if the displayed book has status = vorhanden
What is my mistake?
Thanks in advance Matthias
book.status is a BookStatus instance, not a string. You need to compare book.status.name.