Recently discovered before writing a program there is a problem, and you share the following:
Demand:
The data in the view displays different colors on the page based on the following criteria:
[30,60] Blue
[0,30] Red
(60,100] Green
Previous program fragment:
{% if x.y.5day_ago|floatformat:2 > 60|floatformat:2%}
<TD style= "Color:green" ><strong> {{x.y.5day_ago}}%</strong></td>
{% Else%}
{% if X.y.5day_ago|floatformat:2 < 30|floatformat:2%}
<TD style= "color:red" ><strong>{{x.y.5day_ago}}%</strong></td>
{% Else%}
<TD style= "Color:blue" >{{x.y.5day_ago}}%</td>
{% ENDIF%}
{% ENDIF%}
You can basically implement different colors for different ranges of data.
But later found that the data between [0,10], divided into three paragraphs, there are red, blue, green three different colors, according to the above conditions, 10 of the number within the range of <30, should be displayed in red. The solution is not to be baffled.
In order to solve this problem, we have customized a similar dictionary, the corresponding value is a numeric type, the use of this condition can be displayed normally.
Finally, the type of the X.y.5day_ago similar value is printed out before it is found on the ' str ' type. The filter num.py is created, and the string is converted to floating-point data
From Django Import Template
Register = template. Library ()
@register. Filter (name= ' Numgo ')
def numgo (value):
Return (float (value))
The code in the template changes to:
{% if X.y.5day_ago|numgo > 60|numgo or X.y.5day_ago|numgo = 100|numgo%}
<TD style= "Color:green" ><strong> {{x.y.5day_ago}}%</strong></td>
{% Else%}
{% if X.y.5day_ago|numgo < 30|numgo%}
<TD style= "color:red" ><strong>{{x.y.5day_ago}}%</strong></td>
{% Else%}
<TD style= "Color:blue" >{{x.y.5day_ago}}%</td>
{% ENDIF%}
{% ENDIF%}
Change to this will not appear above the problem, but why the previous [0,10] data between the normal judgment and the rest of the data is not the cause of the problem is not very clear.
Use if tags for data comparison issues in Django templates