How to compare a DecimalField value in Django templates for conditional logic?
I'm working on a Django project where users have an Account
model with a DecimalField
named account_balance
. I'm trying to conditionally show a KYC verification link if the user's balance is exactly 100, and show an error if it's less than that.
Here’s my setup:
class Account(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
account_balance = models.DecimalField(max_digits=10, decimal_places=2)
class KYC(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField()
views.py:
def settings(request):
kycs = KYC.objects.filter(user=request.user)
context = {
"kycs": kycs,
}
return render(request, 'public_pages/settings.html', context)
template:
{% for kyc in kycs %}
<p>Your identity is important to the community</p>
{% if kyc.user.account.account_balance == 100 %}
<a href="{% url 'Kyc_Form' kyc.slug %}">Update KYC</a>
{% elif kyc.user.account.account_balance < 100 %}
<div class="alert alert-danger">
You need at least 100 coins before applying for KYC.
</div>
{% endif %}
{% endfor %}
But this block never renders either the button or the error message. The only thing I see is the static text:
<p>Your identity is important to the community</p>
I tried printing the balance outside the logic:
<p>Balance: {{ kyc.user.account.account_balance }}</p>
And it shows something like 100.00. I assume it's a Decimal issue.
How can I properly compare a DecimalField
like account_balance
in the Django template to check if it equals 100?