I have this code
我有这段代码
{% for account in object_list %}
<tr>
{% for field, value in book.get_fields %}
<th>{{ field.verbose_name }}</th>
{% endfor %}
</tr>
{{ break }}
{% endfor %}
I want to break the for loop after first iteration. break is not working
我想在第一次迭代之后打破for循环。休息不工作
5 个解决方案
#1
14
There is no break
in Django template system. Django template system is not programmed with python but with its own language.
Django模板系统没有中断。Django模板系统不是用python编程的,而是用它自己的语言编写的。
Depending on what you need to do, you might find this question useful. Otherwise, just put the one and only account you are trying to print on HTML on a special field on your RequestContext
.
根据你需要做什么,你可能会发现这个问题很有用。否则,只需将要在HTML上打印的唯一帐户放在RequestContext的一个特殊字段上。
#2
78
I think you should use slice to achieve your goal
我认为你应该用slice来实现你的目标。
{% for account in object_list|slice:":1" %}
#3
2
You can't use break statement but you can choose not to print them on html. It's not a best solution but you can use it. I use the following one;
不能使用break语句,但可以选择不在html上打印它们。这不是最好的解决方案,但你可以使用它。我用下面这个;
{%for tumbnail in image %}
{%if tumbnail.object_id == element.id %}
<img src="/media/{{ tumbnail.image }}" class="tr_all_hover"alt="">
{{ "<!--" }}
{%endif%}
{%endfor%}
{{ "-->" }}
Its basicly seem like this on browser. http://i.stack.imgur.com/MPbR3.jpg
在浏览器上它的基本原理是这样的。http://i.stack.imgur.com/MPbR3.jpg
#4
1
I found a way to do this with a condition. It's ugly and hacky, but it works (for me). first
is what the OP wanted, but this answers the actual question more closely.
我找到了一种方法来解决这个问题。它既丑陋又陈腐,但(对我来说)很管用。首先是OP想要什么,但这更接近实际问题。
Given this:
鉴于这种:
obj = {
'children': [
{ 'possessions' : { 'toys': [] } },
{ 'possessions' : { 'toys': ['train'] } }
{ 'possessions' : { 'toys': ['train', 'ball'] } }
]
}
I wanted to know if my obj has any children with possessions that are toys.
我想知道我的孩子是否有玩具。
Here's what I did:
Python Equivalent:
if ([child for child in obj.children if child.possessions.toys]):
# Whatever
Django Template:
My approach was to use regroup
to build sets of candidates which did or didn't match the criteria:
我的方法是使用regroup建立一组符合或不符合标准的候选人:
{% regroup obj.children by possessions.toys|length_is:"0" as by_toys %}
{% for check in by_toys %}{% if check.grouper == False %}
Whatever
{% endif %}{% endfor %}
regroup
builds a new object that is essentially:
regroup构建的新对象本质上是:
[
{ 'grouper': '', 'list': [/*...*/] },
{ 'grouper': True, 'list': [/*...*/] },
{ 'grouper': False, 'list': [/*...*/] }
]
The length_is:"0"
makes sure that we have at most three elements in that list and the grouper
is either True
or False
or ''
. Then we iterate over the list and check for a False
value.
length_is:“0”确保我们在列表中最多有3个元素,grouper要么是True,要么是False。然后遍历列表并检查错误值。
- If there are no children it'd be an empty list and the
if
would never be hit. - 如果没有孩子,它将是一个空列表,如果永远不会被击中。
- If no children have toys, it'd be a list without a
False
grouper
. - 如果没有孩子有玩具,那就没有假的石斑鱼。
- If all children have toys, it'd be a list with a
False
grouper
. - 如果所有的孩子都有玩具,那就是一张假石斑鱼的清单。
- If some children have toys, it'd be a list with
False
andTrue
grouper
s. - 如果有些孩子有玩具,那就会是一个有假的和真的石斑鱼的清单。
#5
0
You can use your Django template system for loop in javascript for loop as inner loop and can use break as follows :-
您可以使用您的Django模板系统for循环在javascript for循环中作为内部循环,并可以使用break如下:-
for(var i=0;i<1;i++){
{% for owner in Owner %}
id = "{{owner.id}}";
if(id == pk1){
f="{{owner.flat}}";
break;
}
{% endfor %}
}
#1
14
There is no break
in Django template system. Django template system is not programmed with python but with its own language.
Django模板系统没有中断。Django模板系统不是用python编程的,而是用它自己的语言编写的。
Depending on what you need to do, you might find this question useful. Otherwise, just put the one and only account you are trying to print on HTML on a special field on your RequestContext
.
根据你需要做什么,你可能会发现这个问题很有用。否则,只需将要在HTML上打印的唯一帐户放在RequestContext的一个特殊字段上。
#2
78
I think you should use slice to achieve your goal
我认为你应该用slice来实现你的目标。
{% for account in object_list|slice:":1" %}
#3
2
You can't use break statement but you can choose not to print them on html. It's not a best solution but you can use it. I use the following one;
不能使用break语句,但可以选择不在html上打印它们。这不是最好的解决方案,但你可以使用它。我用下面这个;
{%for tumbnail in image %}
{%if tumbnail.object_id == element.id %}
<img src="/media/{{ tumbnail.image }}" class="tr_all_hover"alt="">
{{ "<!--" }}
{%endif%}
{%endfor%}
{{ "-->" }}
Its basicly seem like this on browser. http://i.stack.imgur.com/MPbR3.jpg
在浏览器上它的基本原理是这样的。http://i.stack.imgur.com/MPbR3.jpg
#4
1
I found a way to do this with a condition. It's ugly and hacky, but it works (for me). first
is what the OP wanted, but this answers the actual question more closely.
我找到了一种方法来解决这个问题。它既丑陋又陈腐,但(对我来说)很管用。首先是OP想要什么,但这更接近实际问题。
Given this:
鉴于这种:
obj = {
'children': [
{ 'possessions' : { 'toys': [] } },
{ 'possessions' : { 'toys': ['train'] } }
{ 'possessions' : { 'toys': ['train', 'ball'] } }
]
}
I wanted to know if my obj has any children with possessions that are toys.
我想知道我的孩子是否有玩具。
Here's what I did:
Python Equivalent:
if ([child for child in obj.children if child.possessions.toys]):
# Whatever
Django Template:
My approach was to use regroup
to build sets of candidates which did or didn't match the criteria:
我的方法是使用regroup建立一组符合或不符合标准的候选人:
{% regroup obj.children by possessions.toys|length_is:"0" as by_toys %}
{% for check in by_toys %}{% if check.grouper == False %}
Whatever
{% endif %}{% endfor %}
regroup
builds a new object that is essentially:
regroup构建的新对象本质上是:
[
{ 'grouper': '', 'list': [/*...*/] },
{ 'grouper': True, 'list': [/*...*/] },
{ 'grouper': False, 'list': [/*...*/] }
]
The length_is:"0"
makes sure that we have at most three elements in that list and the grouper
is either True
or False
or ''
. Then we iterate over the list and check for a False
value.
length_is:“0”确保我们在列表中最多有3个元素,grouper要么是True,要么是False。然后遍历列表并检查错误值。
- If there are no children it'd be an empty list and the
if
would never be hit. - 如果没有孩子,它将是一个空列表,如果永远不会被击中。
- If no children have toys, it'd be a list without a
False
grouper
. - 如果没有孩子有玩具,那就没有假的石斑鱼。
- If all children have toys, it'd be a list with a
False
grouper
. - 如果所有的孩子都有玩具,那就是一张假石斑鱼的清单。
- If some children have toys, it'd be a list with
False
andTrue
grouper
s. - 如果有些孩子有玩具,那就会是一个有假的和真的石斑鱼的清单。
#5
0
You can use your Django template system for loop in javascript for loop as inner loop and can use break as follows :-
您可以使用您的Django模板系统for循环在javascript for循环中作为内部循环,并可以使用break如下:-
for(var i=0;i<1;i++){
{% for owner in Owner %}
id = "{{owner.id}}";
if(id == pk1){
f="{{owner.flat}}";
break;
}
{% endfor %}
}