Django template: show price normally, or show discount price with original price struck through below
I’m building a product card in a Django template. I want this behavior:
If
discount_priceis0, show onlyprice.If
discount_priceis not0, showdiscount_priceon top and the originalpricebelow it with a strike-through.
Here is my current template
<div class="flex flex-col">
{% if products.discount_price == 00.00 %}
<span class="font-bold">GH₵{{products.price}}</span>
{% else %}
<span class="line-through">GH₵{{products.discount_price}}</span>
{% endif %}
</div>
There are two issues in your code
1: the use of 00.00 is unnecessary use 0 for a numeric comparison.
2: else block only display discount price, you need to show discount price first and original price below
Use {% if not products.discount_price or products.discount_price == 0 %} to ensure the condition works even if your model field defaults to 0, 0.00, or None (null), this {% else %} block renders products.discount_price as the main bold price, followed by products.price with the line-through