29

I want to put break and continue in my code, but it doesn't work in Django template. How can I use continue and break using Django template for loop. Here is an example:

{% for i in i_range %}
{% for frequency in patient_meds.frequency %}
{% ifequal frequency i %}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}" checked/> {{ i }} AM</td>
{{ forloop.parentloop|continue }} ////// It doesn't work
{ continue }                      ////// It also doesn't work
{% endifequal %}
{% endfor%}
<td class="nopad"><input type="checkbox" name="frequency-1" value="{{ i }}"/> {{ i }} AM</td>
{% endfor %}
Tevin Joseph K O
  • 2,574
  • 22
  • 24
GoldenBird
  • 617
  • 2
  • 9
  • 15
  • You need to be clearer. Is this a python problem or a django template problem? Normally templates shouldn't have such logic in them. Can you post a simple example with some code? – Spacedman Feb 07 '11 at 11:59
  • 2
    You will have to show us the code that does not work and explain how it fails (for example by copying the exact error message into your question). – Sven Marnach Feb 07 '11 at 11:59
  • % for i in i_range %} {% for frequency in patient_meds.frequency %} {% ifequal frequency i %} {{ i }} AM {{ forloop.parentloop|continue }} ////// I does'nt work { continue } ////// It also does'nt work {% endifequal %} {% endfor%} {{ i }} AM {% endfor %} – GoldenBird Feb 07 '11 at 12:20

4 Answers4

39

Django doesn't support it naturally.

You can implement forloop|continue and forloop|break with custom filters.

http://djangosnippets.org/snippets/2093/

Jasim Muhammed
  • 1,376
  • 1
  • 20
  • 28
37

For-loops in Django templates are different from plain Python for-loops, so continue and break will not work in them. See for yourself in the Django docs, there are no break or continue template tags. Given the overall position of Keep-It-Simple-Stupid in Django template syntax, you will probably have to find another way to accomplish what you need.

Gintautas Miliauskas
  • 7,744
  • 4
  • 32
  • 34
  • 4
    thats true, but it's like limitation, not KISS. break is simple. example: stop iterate main loop if any nested loop produce enough items - very usefull and simple with break in template - neat template system like mako or cheetah have support for break/continue. – Sławomir Lenart Sep 16 '14 at 16:28
  • @ups in the example you've given you could simply use the template tag `slice` (example: `list|slice:":10"`) to limit the loop to a certain number of iterations, or even do it at the context level. – gdvalderrama Apr 01 '16 at 16:01
16

For most of cases there is no need for custom templatetags, it's easy:

continue:

{% for each in iterable %}
  {% if conditions_for_continue %}
       <!-- continue -->
  {% else %}
       ... code ..
  {% endif %}
{% endfor %}

break use the same idea, but with the wider scope:

{% set stop_loop="" %}
{% for each in iterable %}
  {% if stop_loop %}{% else %}
       ... code ..
       under some condition {% set stop_loop="true" %}
       ... code ..
  {% endif %}
{% endfor %}

This is Jinja template, which you can easily use within Django b/c Jinja is built in.

You can use even both template backends in the same project (Jinja and Django template).

Sławomir Lenart
  • 7,543
  • 4
  • 45
  • 61
2

If you want a continue/break after certain conditions, I use the following Simple Tag as follows with "Vanilla" Django 3.2.5:

@register.simple_tag
def define(val=None):
  return val

Then you can use it as any variable in the template

{% define True as continue %}
      
{% for u in queryset  %}

  {% if continue %}

    {% if u.status.description == 'Passed' %}
      <td>Passed</td>
      
      {% define False as continue %}

    {% endif %}

  {% endif %}

{% endfor %}

Extremely useful for any type of variable you want to re-use on template without using with statements.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61