【问题标题】:How do I get the first list objects value using a second list objects result in a django template如何使用第二个列表对象获取第一个列表对象值导致 django 模板
【发布时间】:2013-03-24 15:12:46
【问题描述】:

我有两个对象列表:firstobjectlistsecondobjectlist。有了这两个列表,我想使用第二个对象列表结果值来获取第一个对象的值。

例如:

{% for i in firstobjectlist %}
    {% for value in secondobjectlist %}
        <td align="left">{{i{{value.id}}}}</td>
    {% endfor %}
{% endfor %}

当我执行上面的代码时,我得到了错误:

“无法解析余数:'{{value.id' from 'i.{{value.id'”

谁能帮我看看应该怎么做?

【问题讨论】:

  • 我不知道你想在这里做什么。 {{i{{value.id}}}} 是错误的。而是在模板中发送两个单独的列表。为什么不在视图中使用 zip。还要解释你为什么要这样做,这个问题对我来说似乎不清楚 share|edit|delete|flag
  • with {{value.id}} 我会得到一个值让我说 1 这个值我想用作 {{i.1}} 以便我可以显示 {{i.1} 的结果} 在我的 HTML 页面中

标签: django django-templates django-views


【解决方案1】:

首先,django 模板变量必须在{{ 和内容之间有一个空格。

您可以使用the with tag(假设value.id 的值是firstobjectlist 中的键或索引):

{% for i in firstobjectlist %}
    {% for value in secondobjectlist %}
        {% with value.id as j %}
            <td align="left">{{ i.j }}</td>
        {% endwith %}
    {% endfor %}
{% endfor %}

【讨论】:

  • 嗨,蒂米,我无法获得值,请详细解释一下,我认为 {{ ij }} 与 {{ i.value.id}} 相同
  • 我试过了,但我无法得到请帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 2017-06-27
  • 2020-12-26
  • 2020-04-08
  • 2019-04-30
相关资源
最近更新 更多