我想在我的代码中放弃并继续,但它在Django模板中不起作用.如何使用Django模板for循环使用continue和break.这是一个例子:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
Jas*_*med 39
Django自然不支持它.
您可以使用自定义过滤器实现forloop | continue和forloop | break.
http://djangosnippets.org/snippets/2093/
Sła*_*art 12
对于大多数情况,不需要自定义模板标签,这很简单:
继续:
{% for each in iterable %}
{% if conditions_for_continue %}
<!-- continue -->
{% else %}
... code ..
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
break使用相同的想法,但范围更广:
{% set stop_loop="" %}
{% for each in iterable %}
{% if stop_loop %}{% else %}
... code ..
under some condition {% set stop_loop="true" %}
... code ..
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果您接受超出需要的迭代次数。