Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a field/attribute is null and blank in Django?

My payment attribute has a ForeignKey relation. In my accounts.html, I want to show users if they have paid for their orders. So I was trying it like this:

{% if order.payment is null or blank %}
    <td> x</td>
{% else %}
    <td> Yes</td>
{% endif %}

But it didn't work out. What is the proper code for this?

My orders.models.py:

class Order(models.Model):
    payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, blank= True, null=True)

My accounts.html:

{% for order in user.order_set.all %}
    <tbody>
      <tr>
        <th scope="row">{{ forloop.counter }}</th>
          <td>{{ order.order_id }}</td>
            {% if order.payment is null or blank %}
              <td> x</td>
            {% else %}
              <td> Yes</td>
            {% endif %}
            <td>No</td>
        </tr>
    </tbody>
{% endfor %}
like image 455
Jack Avatar asked Sep 03 '25 06:09

Jack


1 Answers

You can simply use {% if not order.payment %}, and I'm pretty sure you can also use {% if not order.payment.exists %}, or {% if order.payment is None %}

like image 128
Hybrid Avatar answered Sep 04 '25 20:09

Hybrid