Just a quick question: is there a way to remove an item from a list in the Django template language?
只是一个简单的问题:有没有办法从Django模板语言中删除列表中的项目?
I have a situation where I'm iterating through one list, and printing the first item in another list. Once the first item is printed I want to remove it from that list.
我有一种情况,我正在迭代一个列表,并在另一个列表中打印第一个项目。打印完第一个项目后,我想将其从该列表中删除。
See below:
{% for item in list1 %}
{{list2.0}}
#remove list2.0 from list2
{% endfor %}
Thanks in advance.
提前致谢。
4 个解决方案
#1
7
If your list1 and list2 are indeed lists and not querysets, this seems to work:
如果你的list1和list2确实是列表而不是查询集,那么这似乎有效:
{{ list2 }} {# show list2 #}
{% for item in list1 %}
{{ list2.0 }}
{# remove list2.0 from list2 #}
{{ list2.pop.0 }}
{% endfor %}
{{ list2 }} {# empty #}
Note that pop
does not return in this case, so you still need {{ list2.0 }} explicitly.
请注意,在这种情况下pop不会返回,因此您仍然需要{{list2.0}}显式。
#2
2
I would try to filter out the item in the view if at all possible. Otherwise you can add in an if or if not statement inside the for loop.
如果可能的话,我会尝试过滤掉视图中的项目。否则,您可以在for循环中添加if或if not语句。
{% for item in list%}
{% if item.name != "filterme" %}
{{ item.name }}
{% endif %}
{% endfor %}
#3
0
You can't delete an item but you can get the list without a certain item (at a constant index)
你不能删除一个项目,但你可以获得没有某个项目的列表(在一个恒定的索引)
{% with list2|slice:"1:" as list2 %}
...
{% endwith %}
Of course, nesting rules apply, etc.
当然,嵌套规则适用等。
In general, I you find yourself doing complex data structure manipulation, just move it to Python - it'd be faster and cleaner.
一般来说,我发现自己正在进行复杂的数据结构操作,只需将其移动到Python - 它就会更快更清洁。
#4
0
There is no such built-in template tag. I understand that you don't want to print first item of list2
if list1
is not empty. Try:
没有这样的内置模板标签。据我所知,如果list1不为空,你不想打印list2的第一项。尝试:
{% for item in list1 %}
{{list2.0}}
...
{% endfor %}
{% for item in list2 %}
{% if list1 and forloop.counter == 1 %}
# probably pass
{% else %}
{{ item }}
{% endif %}
{% endfor %}
This is not a good idea to manipulate the content of the list in templates.
在模板中操作列表的内容不是一个好主意。
#1
7
If your list1 and list2 are indeed lists and not querysets, this seems to work:
如果你的list1和list2确实是列表而不是查询集,那么这似乎有效:
{{ list2 }} {# show list2 #}
{% for item in list1 %}
{{ list2.0 }}
{# remove list2.0 from list2 #}
{{ list2.pop.0 }}
{% endfor %}
{{ list2 }} {# empty #}
Note that pop
does not return in this case, so you still need {{ list2.0 }} explicitly.
请注意,在这种情况下pop不会返回,因此您仍然需要{{list2.0}}显式。
#2
2
I would try to filter out the item in the view if at all possible. Otherwise you can add in an if or if not statement inside the for loop.
如果可能的话,我会尝试过滤掉视图中的项目。否则,您可以在for循环中添加if或if not语句。
{% for item in list%}
{% if item.name != "filterme" %}
{{ item.name }}
{% endif %}
{% endfor %}
#3
0
You can't delete an item but you can get the list without a certain item (at a constant index)
你不能删除一个项目,但你可以获得没有某个项目的列表(在一个恒定的索引)
{% with list2|slice:"1:" as list2 %}
...
{% endwith %}
Of course, nesting rules apply, etc.
当然,嵌套规则适用等。
In general, I you find yourself doing complex data structure manipulation, just move it to Python - it'd be faster and cleaner.
一般来说,我发现自己正在进行复杂的数据结构操作,只需将其移动到Python - 它就会更快更清洁。
#4
0
There is no such built-in template tag. I understand that you don't want to print first item of list2
if list1
is not empty. Try:
没有这样的内置模板标签。据我所知,如果list1不为空,你不想打印list2的第一项。尝试:
{% for item in list1 %}
{{list2.0}}
...
{% endfor %}
{% for item in list2 %}
{% if list1 and forloop.counter == 1 %}
# probably pass
{% else %}
{{ item }}
{% endif %}
{% endfor %}
This is not a good idea to manipulate the content of the list in templates.
在模板中操作列表的内容不是一个好主意。