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 %}
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 %}
blank is not the state of a field. If a field is blank, then it means that no value is required to be passed. The for example a default value is used.
You can check if a value is NULL
by checking if the value is None
:
{% if order.payment %}
…
{% endif %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.