Python Django模板按索引获取字典列表中的元素

Python Django模板按索引获取字典列表中的元素,python,django,django-templates,Python,Django,Django Templates,我有以下资料: item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}] item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}] def get_context_data(self, **kwargs

我有以下资料:

item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
    item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
    import itertools
    context['zip_longest'] = itertools.zip_longest(item0, item1)
    return context
在django模板中,我想按索引显示每个列表元素的价格:15.52、12.55、31.2、34.1、1.2

列表大小可能不相等,因此我将发送最大列表的大小

在最大列表大小上迭代:

{i.item | index:forloop.counter0}}
获取我
{'itemCode':'AZ001','price':15.52}

如果我想要价格,我能做什么

执行
{i.item | index:forloop.counter0.price}}
给我的是索引0处的无效键价格

换句话说,我按列顺序发送元素,并希望按行顺序显示它们,而无需在服务器上使用zip进行列表理解


有什么解决办法吗?

我不确定你的问题是否正确,但这就是你要的代码

views.py

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['item'] = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}]
        return context
template.html

{{ item.0.price }}
{% for element in zip_longest %}
    {% for item in element %}
        {% if item %}
            {{ item.price }} <br>
        {% endif %}
    {% endfor %}
{% endfor %}
结果为15.52


如果要在其上循环,可以这样做:

{% for i in item %}
    {{ i.price }}
{% endfor %}

在您更新问题后,我将执行以下操作:

item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
    item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
    import itertools
    context['zip_longest'] = itertools.zip_longest(item0, item1)
    return context
template.html

{{ item.0.price }}
{% for element in zip_longest %}
    {% for item in element %}
        {% if item %}
            {{ item.price }} <br>
        {% endif %}
    {% endfor %}
{% endfor %}

在我看来,使用它没有什么错,因为它从生成器生成值。

不确定我是否正确回答了你的问题,但这就是你要的代码

<ul>
     {% for key, value in dictionary.items %}
     <li><a href="{{key}}">{{value}}</a></li>
     {% endfor %}
</ul>
views.py

def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['item'] = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}]
        return context
template.html

{{ item.0.price }}
{% for element in zip_longest %}
    {% for item in element %}
        {% if item %}
            {{ item.price }} <br>
        {% endif %}
    {% endfor %}
{% endfor %}
结果为15.52


如果要在其上循环,可以这样做:

{% for i in item %}
    {{ i.price }}
{% endfor %}

在您更新问题后,我将执行以下操作:

item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    item0 = [{'itemCode': 'AZ001', 'price': 15.52}, {'itemCode': 'AB01', 'price': 31.2}, {'itemCode': 'AP01', 'price': 1.2}]
    item1 = [{'itemCode': 'BZ001', 'price': 12.55}, {'itemCode': 'BB01', 'price': 34.1}]
    import itertools
    context['zip_longest'] = itertools.zip_longest(item0, item1)
    return context
template.html

{{ item.0.price }}
{% for element in zip_longest %}
    {% for item in element %}
        {% if item %}
            {{ item.price }} <br>
        {% endif %}
    {% endfor %}
{% endfor %}
在我看来,使用它没有什么错,因为它从生成器生成值。

    <ul>
         {% for key, value in dictionary.items %}
         <li><a href="{{key}}">{{value}}</a></li>
         {% endfor %}
    </ul>
    
    {%为键,dictionary.items%中的值}
  • {%endfor%}
试着用这个,

    {%为键,dictionary.items%中的值}
  • {%endfor%}

尝试使用此选项,

您可能需要提供更多上下文。通常,您不需要使用模板标记来获取基于forloop.counter0的索引,而是直接迭代项:
{%forelem in i.item%}。。。{{elem.price}}
。为什么你不能这样做?我有多个列表,我正在相互比较。因此,当我以列为基础发送它们时,我需要以行为基础显示它们。我可以在服务器上执行zip,但在执行zip之前,我正在尝试其他方法。此外,如果您尝试访问列表的第一个元素,则可以执行:
item.0.price
。您可以编写一个。然而,对我来说,使用
zip
(或
zip\u longest
来处理不同长度的列表)似乎是一种更好的方法。您可能需要提供更多的上下文。通常,您不需要使用模板标记来获取基于forloop.counter0的索引,而是直接迭代项:
{%forelem in i.item%}。。。{{elem.price}}
。为什么你不能这样做?我有多个列表,我正在相互比较。因此,当我以列为基础发送它们时,我需要以行为基础显示它们。我可以在服务器上执行zip,但在执行zip之前,我正在尝试其他方法。此外,如果您尝试访问列表的第一个元素,则可以执行:
item.0.price
。您可以编写一个。但是,对我来说,使用
zip
(或
zip\u longest
来显示不同长度的列表)似乎是更好的方法。编辑问题以解释更多编辑问题以解释更多