循环变量的Django模板标记{{for/empty}}
Django template tags {{for/empty}} for loop variable
要从Django应用程序中生成一组带有相关参数的Javascript变量,我有两个嵌套的for循环:
<script>
{% for model in models %}
{% for item in model.attribute|slice:":3" %}
{% if forloop.first %}
var js_variable{{ forloop.parentloop.counter0 }} = [
{% endif %}
'{{ item.attribute }}' ,
{% if forloop.last %}
{{ item.attribute }} ]
{% empty %}
var js_variable{{ forloop.parentloop.counter0 }} = []
{% endfor %}
{% endfor %}
....code that gets unhappy when js_variable[n] doesn't exist.....
</script>
当{% empty %}
出现时,它似乎无法访问{{ forloop.parentloop. counter0 }}
变量,因此变量名js_variable[n]
被错误地打印为js_variable
(没有计数器提供的数字),随后代码会发出抱怨。
这个变量在{{ empty }}
标记中是否不可用?
这是预期的行为。简化我们有:
{% for A ... %}
{{ forloop.* }} is there for the 'for A ...'
{% for B ... %}
{{ forloop.* }} is there for the 'for B ...'
{{ forloop.parentloop.* }} refers to the 'for A ...'
{% empty %}
{{ forloop.* }} is there for the 'for A ...' !!!
{% endfor %}
{% endfor %}
在{%empty%}中,{{forloop}}引用父forloop!更改:
var js_variable{{ forloop.parentloop.counter0 }} = []
带有:
var js_variable{{ forloop.counter0 }} = []
相关文章:
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Is onfling available for html, html5
- Setting default onclick behavior for <img> tag in gene
- 我的javascript for循环不起作用
- For循环冻结Javascript
- 如何在for循环中添加事件侦听器
- Javascript setTimeout for an array
- 双“for”循环(循环)
- 引用vue.js中v-for中的上一个值
- javascript for循环不起作用
- for循环中的javascript if语句找不到==
- GWT (or javascript) library for couchDB
- Javascript在for循环中等待处理请求
- Livereload for node.js.有可能吗
- jQuery setTimeOut: in for-loop
- 我没有'I don’我不理解for在循环中的用途
- 如何知道javascript for语句中的所有结果是否都是错误的
- 循环变量的Django模板标记{{for/empty}}
- Twitter API getJSON for Status ID Meta -- Response Empty!