Django - 如何在模板中进行元组解包“for” 环形
在我的views.py中,我正在构建一个双元组列表,其中元组中的第二项是另一个列表,如下所示:
[ Product_Type_1, [ product_1, product_2 ],
Product_Type_2, [ product_3, product_4 ]]
在普通的旧Python中,我可以像这样迭代列表:
for product_type, products in list:
print product_type
for product in products:
print product
我似乎无法在我的 Django 模板中执行相同的操作:
{% for product_type, products in product_list %}
print product_type
{% for product in products %}
print product
{% endfor %}
{% endfor %}
我从 Django 收到此错误:
渲染时捕获异常:zip 参数 #2 必须支持迭代
当然,模板中有一些 HTML 标记,而不是打印声明。 Django 模板语言不支持元组解包吗? 或者我以错误的方式处理这个问题? 我想做的就是显示一个简单的对象层次结构 - 有多种产品类型,每种类型都有多种产品(在 models.py 中,Product 有一个 Product_type 的外键,一个简单的一对多关系)。
显然,我对 Django 还很陌生,所以任何意见都将不胜感激。
In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this:
[ Product_Type_1, [ product_1, product_2 ],
Product_Type_2, [ product_3, product_4 ]]
In plain old Python, I could iteration the list like this:
for product_type, products in list:
print product_type
for product in products:
print product
I can't seem to do the same thing in my Django template:
{% for product_type, products in product_list %}
print product_type
{% for product in products %}
print product
{% endfor %}
{% endfor %}
I get this error from Django:
Caught an exception while rendering: zip argument #2 must support iteration
Of course, there is some HTML markup in the template, not print statements. Is tuple unpacking not supported in the Django template language? Or am I going about this the wrong way? All I am trying to do is display a simple hierarchy of objects - there are several product types, each with several products (in models.py, Product has a foreign key to Product_type, a simple one-to-many relationship).
Obviously, I am quite new to Django, so any input would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一种方式如下。
如果有一个元组列表,例如:
那么可以通过以下方式在模板文件中解压该列表。
就我而言,我有一个元组列表,其中包含文档的 URL、标题和摘要。
Another way is as follows.
If one has a list of tuples say:
then one can unpack this list in the template file in the following manner.
In my case I had a list of tuples which contained the URL, title, and summary of a document.
最好是像这样构建数据{注意“(”和“)”可以分别交换为“[”和“]”,一个用于元组,一个用于列表}
并让模板执行以下操作
:在 for 循环中解包元组/列表的方式基于列表迭代器返回的项目。
每次迭代仅返回一项。 第一次循环时,Product_Type_1,第二次是您的产品列表...
it would be best if you construct your data like {note the '(' and ')' can be exchanged for '[' and ']' repectively, one being for tuples, one for lists}
and have the template do this:
the way tuples/lists are unpacked in for loops is based on the item returned by the list iterator.
each iteration only one item was returned. the first time around the loop, Product_Type_1, the second your list of products...
你必须这样使用:
不要忘记字典数据中的变量项
You must used this way:
Don't forget the variable items in the dictionary data
如果元组中有固定数量,则可以仅使用索引。 我需要混合字典并且值是元组,所以我这样做了:
在视图中:
在模板中:
If you have a fixed number in your tuples, you could just use indexing. I needed to mix a dictionary and the values were tuples, so I did this:
In the view:
In the template:
只需向模板发送产品类型列表,然后执行以下操作:
已经有一段时间了,所以我记不清语法是什么,请告诉我这是否有效。 查看文档。
Just send the template a list of product types and do something like:
It's been a little while so I can't remember exactly what the syntax is, let me know if that works. Check the documentation.