<legend id='LVXvr'><style id='LVXvr'><dir id='LVXvr'><q id='LVXvr'></q></dir></style></legend>

    <bdo id='LVXvr'></bdo><ul id='LVXvr'></ul>

<small id='LVXvr'></small><noframes id='LVXvr'>

    <i id='LVXvr'><tr id='LVXvr'><dt id='LVXvr'><q id='LVXvr'><span id='LVXvr'><b id='LVXvr'><form id='LVXvr'><ins id='LVXvr'></ins><ul id='LVXvr'></ul><sub id='LVXvr'></sub></form><legend id='LVXvr'></legend><bdo id='LVXvr'><pre id='LVXvr'><center id='LVXvr'></center></pre></bdo></b><th id='LVXvr'></th></span></q></dt></tr></i><div id='LVXvr'><tfoot id='LVXvr'></tfoot><dl id='LVXvr'><fieldset id='LVXvr'></fieldset></dl></div>

    1. <tfoot id='LVXvr'></tfoot>

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

      Django - How to do tuple unpacking in a template #39;for#39; loop(Django - 如何在模板“for循环中进行元组解包)
        <tbody id='FisF2'></tbody>

        • <bdo id='FisF2'></bdo><ul id='FisF2'></ul>
          <i id='FisF2'><tr id='FisF2'><dt id='FisF2'><q id='FisF2'><span id='FisF2'><b id='FisF2'><form id='FisF2'><ins id='FisF2'></ins><ul id='FisF2'></ul><sub id='FisF2'></sub></form><legend id='FisF2'></legend><bdo id='FisF2'><pre id='FisF2'><center id='FisF2'></center></pre></bdo></b><th id='FisF2'></th></span></q></dt></tr></i><div id='FisF2'><tfoot id='FisF2'></tfoot><dl id='FisF2'><fieldset id='FisF2'></fieldset></dl></div>
          1. <small id='FisF2'></small><noframes id='FisF2'>

          2. <tfoot id='FisF2'></tfoot>

            1. <legend id='FisF2'><style id='FisF2'><dir id='FisF2'><q id='FisF2'></q></dir></style></legend>
              • 本文介绍了Django - 如何在模板“for"循环中进行元组解包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在我的views.py中,我正在构建一个双元组列表,其中元组中的第二项是另一个列表,如下所示:

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

                在普通的旧 Python 中,我可以像这样迭代列表:

                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
                

                我似乎无法在我的 Django 模板中做同样的事情:

                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 %}
                

                我从 Django 收到此错误:

                I get this error from Django:

                渲染时遇到异常:zip 参数 #2 必须支持迭代

                当然,模板中有一些 HTML 标记,而不是打印语句.Django 模板语言不支持元组解包吗?还是我以错误的方式解决这个问题?我要做的只是显示一个简单的对象层次结构 - 有几种产品类型,每种都有几个产品(在 models.py 中,Product 有一个 Product_type 的外键,一个简单的一对多关系).

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

                显然,我对 Django 还是很陌生,因此我们将不胜感激.

                Obviously, I am quite new to Django, so any input would be appreciated.

                推荐答案

                最好像{note the '(' and ')' can be exchangely for '[' and ']' 这样构建数据,一个用于元组,一个用于列表}

                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 )) ]
                

                并让模板执行此操作:

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

                元组/列表在 for 循环中解包的方式基于列表迭代器返回的项目.每次迭代只返回一项.第一次循环,Product_Type_1,第二次你的产品列表......

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

                这篇关于Django - 如何在模板“for"循环中进行元组解包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    <small id='JPhw3'></small><noframes id='JPhw3'>

                    <tfoot id='JPhw3'></tfoot>
                      <tbody id='JPhw3'></tbody>

                    <legend id='JPhw3'><style id='JPhw3'><dir id='JPhw3'><q id='JPhw3'></q></dir></style></legend>

                      <bdo id='JPhw3'></bdo><ul id='JPhw3'></ul>

                        <i id='JPhw3'><tr id='JPhw3'><dt id='JPhw3'><q id='JPhw3'><span id='JPhw3'><b id='JPhw3'><form id='JPhw3'><ins id='JPhw3'></ins><ul id='JPhw3'></ul><sub id='JPhw3'></sub></form><legend id='JPhw3'></legend><bdo id='JPhw3'><pre id='JPhw3'><center id='JPhw3'></center></pre></bdo></b><th id='JPhw3'></th></span></q></dt></tr></i><div id='JPhw3'><tfoot id='JPhw3'></tfoot><dl id='JPhw3'><fieldset id='JPhw3'></fieldset></dl></div>