1

My code is:

{% for key, value in section.items %}
    {% for key_t, value_t in title.items %}
            {% if value_t.section_id == key|add:"0" %}
                 <li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs"> 
                 {{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
            {% endif %}
    {% endfor %}
{% endfor %}

I want to break the for loop when if the condition is true. like as

{% for key, value in section.items %}
    {% for key_t, value_t in title.items %}
            {% if value_t.section_id == key|add:"0" %}
                 <li class="nav-item-header"><div class="text-uppercase font-size-xs line-height-xs"> 
                 {{value.title}}</div> <i class="icon-menu" title="Tables"></i></li>
            {{break}}
            {% endif %}
    {% endfor %}
{% endfor %}

How is it possible? please help me...

M N Tushar
  • 258
  • 1
  • 5
  • 16
  • Im having trouble understanding your iterations. can you explain what they are intended for? – Chris Nov 09 '20 at 09:26

1 Answers1

-1

There is no way to break out of a for loop in Django Template. However, you can achieve this by setting a variable and adding an if statement on the top like this.

{% set isBreak = False %}
{% for number in numbers %}
{% if 99 == number %}
    {% set isBreak = true %}
{% endif %}

{% if isBreak %}
    {# this is a comment. Do nothing. #}
{% else %}
    <div>{{number}}</div>
{% endif %}
{% endfor %}

for some additional help check out this link https://dev.to/anuragrana/for-loops-in-django-2jdi or check this answer on stack overflow How to break "for loop" in Django template

Chris
  • 704
  • 1
  • 11
  • 32
  • 1
    But Django doesn't have a "set" tag... at least it says it doesn't. What am I missing? – evan Dec 05 '22 at 20:31
  • Your answer is false. There is no `set` tag in django template. – parmer_110 Mar 25 '23 at 20:02
  • There is no set tag in Django templates, as has been said by other users, if you have a way of implementing set in django templates please include that in this answer. – BMoe872 May 23 '23 at 01:43