Search code examples
pythonjinja2

unable want to check length of list, i want to check list length greater than 0 than loop over it and render each element


Trying to loop over a list , and then show each element of that list in span element, but its not working first I want to check if length of that list is greater than 0 than loop over the list , I am not familiar with syntax , need help .

code:

  {%if len(prediction)>0%}
  {%for predict in prediction%}
  <span>{{predict}}</span>
  ​{% endfor %}

Solution

  • {% if prediction|length > 0 %}
      {% for predict in prediction %}
        <span>{{predict}}</span>
      ​{% endfor %}
    {% endif %}
    

    Try this, you can use "|length" to get length, if you want equal can use "length_is"

    Like this:

    {{ value|length_is:"0" }}