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 %}
  • 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 7 '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 7 '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 7 '11 at 12:20
up vote 27 down vote accepted

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.

  • 2
    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. – guival Apr 1 '16 at 16:01

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 %}

if you accept iterating more than needed.

Django doesn't support it naturally.

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

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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