如何在Django模板中使用break和continue? [英] How can I use break and continue in Django templates?
问题描述
{%for i in i_range%}
{%for patient_meds.frequency% }
{%ifequal frequency i%}
< td class =nopad>< input type =checkboxname =frequency-1value ={{i}}检查/> {{i}} AM< / td>
{{forloop.parentloop | continue}} //////它不工作
{continue} //////它也不工作
{%endifequal %}
{%endfor%}
< td class =nopad>< input type =checkboxname =frequency-1value ={{i}} > {{i}} AM< / td>
{%endfor%}
Django模板中的循环与纯Python for循环不同,因此 continue
和 break
将无法正常工作。在Django docs 中查看自己,没有 break
或 continue
模板标签。鉴于Django模板语法中的Keep-It-Simple-Stupid的整体位置,您可能需要找到另一种方式来完成所需的操作。
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 %}
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.
这篇关于如何在Django模板中使用break和continue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!