Issue
I am trying to compare two variables using if template tag. If condition is not working as stock.id is integer and form.product_in_stock.value is string.
{% if form.product_in_stock.value == stock.id %}selected{% endif %}
Is there any way to type cast and then compare using if in django template.
Solution
In Django templates, you cannot directly type-cast variables or use complex operations like type casting within the template language. However, you can achieve your goal by converting one of the variables to the same type as the other for the purpose of the comparison.
Assuming that stock.id
is an integer and form.product_in_stock.value
is a string, you can convert stock.id
to a string for the comparison. Here's an example:
{% if form.product_in_stock.value == stock.id|stringformat:"d" %}
selected
{% endif %}
In this example, stock.id|stringformat:"d"
is used to convert the integer stock.id
to a string before comparing it with form.product_in_stock.value
.
Answered By - Arun
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.