Django - 如何在模板中进行元组解包“for” 环形

发布于 2024-07-08 04:36:12 字数 851 浏览 9 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉默的熊 2024-07-15 04:36:12

另一种方式如下。

如果有一个元组列表,例如:

mylst = [(a, b, c), (x, y, z), (l, m, n)]

那么可以通过以下方式在模板文件中解压该列表。
就我而言,我有一个元组列表,其中包含文档的 URL、标题和摘要。

{% for item in mylst %}    
     {{ item.0 }} {{ item.1}} {{ item.2 }}    
{% endfor %}

Another way is as follows.

If one has a list of tuples say:

mylst = [(a, b, c), (x, y, z), (l, m, n)]

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 item in mylst %}    
     {{ item.0 }} {{ item.1}} {{ item.2 }}    
{% endfor %}
陪我终i 2024-07-15 04:36:12

最好是像这样构建数据{注意“(”和“)”可以分别交换为“[”和“]”,一个用于元组,一个用于列表}

[ (Product_Type_1, ( product_1, product_2 )),
   (Product_Type_2, ( product_3, product_4 )) ]

并让模板执行以下操作

{% for product_type, products in product_type_list %}
    {{ product_type }}
    {% for product in products %}
        {{ product }}
    {% endfor %}
{% endfor %}

:在 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}

[ (Product_Type_1, ( product_1, product_2 )),
   (Product_Type_2, ( product_3, product_4 )) ]

and have the template do this:

{% for product_type, products in product_type_list %}
    {{ product_type }}
    {% for product in products %}
        {{ product }}
    {% endfor %}
{% endfor %}

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...

家住魔仙堡 2024-07-15 04:36:12

你必须这样使用:

{% for product_type, products in product_list.items %}
    {{ product_type }}
    {% for product in products %}
       {{ product }}
    {% endfor %}
{% endfor %}

不要忘记字典数据中的变量项

You must used this way:

{% for product_type, products in product_list.items %}
    {{ product_type }}
    {% for product in products %}
       {{ product }}
    {% endfor %}
{% endfor %}

Don't forget the variable items in the dictionary data

雨的味道风的声音 2024-07-15 04:36:12

如果元组中有固定数量,则可以仅使用索引。 我需要混合字典并且值是元组,所以我这样做了:

在视图中:

my_dict = {'parrot': ('dead', 'stone'), 'lumberjack': ('sleep_all_night', 'work_all_day')}

在模板中:

<select>
  {% for key, tuple in my_dict.items %}
    <option value="{{ key }}" important-attr="{{ tuple.0 }}">{{ tuple.1 }}</option>
  {% endfor %}
</select>

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:

my_dict = {'parrot': ('dead', 'stone'), 'lumberjack': ('sleep_all_night', 'work_all_day')}

In the template:

<select>
  {% for key, tuple in my_dict.items %}
    <option value="{{ key }}" important-attr="{{ tuple.0 }}">{{ tuple.1 }}</option>
  {% endfor %}
</select>
披肩女神 2024-07-15 04:36:12

只需向模板发送产品类型列表,然后执行以下操作:

{% for product_type in product_type_list %}
    {{ product_type }}
    {% for product in product_type.products.all %}
        {{ product }}
    {% endfor %}
{% endfor %}

已经有一段时间了,所以我记不清语法是什么,请告诉我这是否有效。 查看文档

Just send the template a list of product types and do something like:

{% for product_type in product_type_list %}
    {{ product_type }}
    {% for product in product_type.products.all %}
        {{ product }}
    {% endfor %}
{% endfor %}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文