1

I first use widthratio tag to calculate a ratio percentage number and assgign it to a variable named Ratio Then when I try to compare it with an integer in if and elif tag, the if and elif tag seems all goes false, and I alway go to the final else tag and get the wrong result. Below is my source code:

     <div class="container-fluid">
        <p><span class="icon-group"></span> Player registratered:  {{event.Players_registratered}}/{{event.Max_players}}</p>
        {% widthratio event.Players_registratered event.Max_players 100 as Ratio %}
        {{Ratio}}
        <div class="progress progress-striped active text-center">
          {% if Ratio < 30 %}
            <div class="progress-bar progress-bar-info" role="progressbar"
                        aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:{{Ratio}}%">
            </div>
          {% elif Ratio < 70 %}
            <div class="progress-bar progress-bar-success" role="progressbar"
                        aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:{{Ratio}}%">
            </div>
          {% elif Ratio < 90 %}
            <div class="progress-bar progress-bar-warning" role="progressbar"
                        aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:{{Ratio}}%">
            </div>
          {% else %}
            <div class="progress-bar progress-bar-danger" role="progressbar"
                        aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:{{Ratio}}%">
            </div>
          {% endif %}
        </div>
      </div>

and the output snip:Wrong output

I hope someone could answer it. Thank you very much.

6
  • The screenshot looks fine, what exactly is not correct about it? If you went to the last else condition each time, all bars would be 100%.
    – dirkgroten
    Commented Mar 17, 2019 at 17:17
  • is Ratio a Number? The if elif would always be wrong if Ratio is a String
    – H4kor
    Commented Mar 17, 2019 at 17:23
  • The two variables I used in the Widthratio tag are integer in the models.py And I am not sure if it is cast to String or not in the tag when I assign the result to Ratio. By the way, when I use {{Ratio | add :"5"}} it will give me right calculate answer. Commented Mar 17, 2019 at 17:35
  • Why you don't use with in the beginning of your withratio tag ? like this: {% with widthratio event.Players_registratered event.Max_players 100 as Ratio %} Commented Mar 17, 2019 at 17:38
  • Thanks gunner. I use with and endwith tag before 'if' and 'endif', but the problem still exist. The variable Ratio seems exist all the way with out using 'with' tag. Commented Mar 17, 2019 at 17:58

1 Answer 1

0

I solved exactly the same issue with:

@register.filter
def to_int(value):
    return int(value)
1
  • the value returned by widthratio is a string
    – The Dev
    Commented Apr 10 at 3:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.