python - django模板中for循环中的“and”条件

标签 python django django-template-filters

嗨,我想在 django 模板内的 for 循环中检查 2 个或多个条件。我试过了 -

<table>
                {% for p, q in products, quantity %}
                    <tr>
                        <td>{{ p.p_name }}</td>
                        <td>{{ q }}</td>
                    </tr>
                {% endfor %}
            </table>

但它没有帮助并给出错误 -

'for' statements should use the format 'for x in y': for p, q in products, quantity

知道如何完成任务..??

提前致谢。

最佳答案

您可以使用 zip() Python 函数在 View 上下文中提供元组列表。然后您可以使用 {% for p, q in new_list %} 循环,其中 new_list 是压缩列表。

关于python - django模板中for循环中的“and”条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411634/

相关文章:

python - django.core.exceptions.ImproperlyConfigured : Field name `id` is not valid for model

python - Django - 呈现为字符串无法加载 CSS

Django:如何将我的基于函数的 View 重新创建为(通用编辑)基于类的 View

python - 如何在一个 View 中调用多个函数

python - 如何在 Django 模板中设置自定义 forloop 起点

python - 仅在 django 模板中显示唯一对象

python - 如何使用 networkx 在图形中显示节点名称?

python - 如何在 Django View 中解析 JSON

python - 为什么 "Broken pipe"只有在多进程的特定场景下访问共享列表时才会出现?

django - 在 Django 模板中使用 JSON