如何在Django模板中使用break和continue? [英] How can I use break and continue in Django templates?

查看:3027
本文介绍了如何在Django模板中使用break和continue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的代码中放弃并继续,但它在Django模板中不起作用。如何使用继续,并使用Django模板进行循环。以下是一个例子:

  {%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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆