Django模板forloopcounter值不起作用

时间:2015-06-25 09:44:36

标签: python django django-templates

列表如下。这里forloopcounter值不起作用。怎么修?请让我知道你的看法。请注意:静态索引值正常,但动态索引值不起作用(forloopcounter)

value = [
    {
        "pk": 1,
        "Rate": [
            {
                "one": "one1"
            },
            {
                "two": "two1"
            }
        ]
    },
    {
        "pk": 2,
        "Rate": [
            {
                "one": "one2"
            },
            {
                "two": "two2"
            }
        ]
    }
]


{% for i in value %}

{{i.Rate.forloopcounter.one}} # here forloopcounter value is not working
{{i.Rate.0.one}} # This is working properly. But i need dynamic index

{% endfor %}

1 个答案:

答案 0 :(得分:0)

I don't think you're using what you think you're using. {% for i in dict %} {{forloop.counter}} forloop.counter increments on each iteration through your dictionary. forloopcounter is not a key in your dictionary, which is why your code doesn't work.