0

I have below sample structure

'_id': 1
'company': 'XXX'
'numberOfProducts': 2
'products': [
        {'sku': 'cnx1cs', 'name': 'canon', 'qty': 3},
        {'sku': 'nkx1cs', 'name': 'nikon', 'qty': 2}
         ]

When I use this {{list_of_assets.products.0.name}} notation inside of the html file (in Django), it seems much more comfortable instead of using dict.items()/dict.iteritems() especially when you think much more nested arrays.

But I could not figure out how I can control array item number inside of the for loop...

I mean if we think above sample, how can I iterate for loop two times and produce below notation by somehow using variable inside of the tag

    {{list_of_assets.products.0.name}}
    ....
    {{list_of_assets.products.1.name}}

regards

4

2 回答 2

0

你可以做

{% for product in list_of_assets.products %}
  {{product.name}}
{% endfor %}
于 2013-06-09T21:07:40.240 回答
0

Django 模板不允许复杂的循环结构或编程。(有关模板 Philosophy 的完整详细信息,请阅读: http: //www.djangobook.com/en/2.0/chapter04.html部分:Philosophies and Limitations。

也就是说,显示数组中所有项目的唯一方法是使用上面建议的解决方案 karthikr:

{%for p in list_of_assets.products %}
    {{p.name}}
{% empty %}
    Nothing in the list!
{% endfor %}
于 2013-06-10T03:14:38.273 回答