如何在 Django 模板中迭代查询集列表 [英] How to iterate a queryset list in a django template
本文介绍了如何在 Django 模板中迭代查询集列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在模板中迭代这个查询集列表?
Is there a way to iterate this list of querysets in the template?
[<Director: Roman Polanski>, <Director: Alfred Hitchcock>,
<Director: Steven Spielberg>, <Director: David Lynch>]
我尝试使用列表语法,但 django 模板语言似乎也不接受列表.谢谢大家.
I tried using a list syntax, but the django template language doesn't seem to accept lists, also. Thank you all.
推荐答案
Django 的模板语言当然接受列表!
Django's template language does of course accept lists!
模板中的代码如下所示:
Here is what the code in your template should look like:
{% for director in director_list %}
{{ director }}
{% endfor %}
顺便说一句:你在这里拥有的是一个查询集(它被评估为一个列表),而不是一个查询集列表.
By the way: What you're having here is a queryset (that gets evaluated to a list), not a list of querysets.
这篇关于如何在 Django 模板中迭代查询集列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文