Use if tags for data comparison issues in Django templates

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.