pipikej
pipikej

Reputation: 305

condition in html template

Hello I am working on the web app with Python Django and I am struggling to reate condition in my html template.

I got table and I want to make the text bold in the cell if the text in the cell is equals to specified text. I tried this:

<table>
    <tr>
       <td>{% if order.order_buffer == 'Buffer' %}{{ order.order_buffer }}{% else %}<b>{{ order.order_buffer }}</b>{% endif %}</td>
    </tr>
</table>

models.py

class Order(models.Model):
    ...
    order_buffer = models.ForeignKey(Buffer, on_delete=models.CASCADE)
 class Buffer(models.Model):
    buffer = models.CharField(max_length=15)   

views.py

class OrderIndex(generic.ListView):
     template_name = 'new_orders/order-list.html'
     def get_queryset(self):
         return Order.objects.all().order_by('-id')
     paginate_by = 50

In this condition it goes straight to the else block. Any ideas?

Upvotes: 1

Views: 392

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

In that case, you should do the opposite, so:

{% if order.order_buffer.buffer == 'Buffer' %}
    <b>{{ order.order_buffer }}</b>
{% else %}
    {{ order.order_buffer }}
{% endif %}

Right now you put in boldface everything except the `'Buffer' text.

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599956

order_buffer is a ForeignKey. It will never be equal to the string "Buffer". That data is in the buffer field of the related model.

{% if order.order_buffer.buffer == 'Buffer' %}

Upvotes: 1

Related Questions