[英]How to compare float variable in django template?
Attendances is a list of float variables. 出勤是浮点变量的列表。 Even when the value is less than 75, the first case is executed.
即使该值小于75,也会执行第一种情况。 I know that by default the django template see's a variable as a string.
我知道,默认情况下,Django模板将变量视为字符串。 I know how to convert the variable to integer by making it {% attendance|add:0 %}, but what do I do when the values are float?
我知道如何通过使其变为{%Attendance | add:0%}将变量转换为整数,但是当值浮点时该怎么办?
{%for attendance in attendances %}
{%if attendance >= 75 %}
<td><p style="color:green;">{{attendance}}</p></td>
{%else%}
<td><p style="color:red;">{{attendance}}</p></td>
{%endif%}
{% endfor %}
I would recommend to construct your own template tag. 我建议构造您自己的模板标签。 You can learn how to here .
您可以在这里学习如何。
Bellow you may find an example of custom tag for random number in range 在下面,您可能会找到范围内随机数的自定义标签示例
import random
from django import template
register = template.Library()
@register.simple_tag
def random_int(a, b=None):
if b is None:
a, b = 0, a
return random.randint(a, b)
The you can use in your template as follows 您可以在模板中使用如下
{% load random_numbers %}
<p>A random value, 1 ≤ {% random_int 1 10 %} ≤ 10.</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.