How to access array elements in a Django template?

前端 未结 4 486
慢半拍i
慢半拍i 2020-11-28 18:27

I am getting an array arr passed to my Django template. I want to access individual elements of the array in the array (e.g. arr[0], arr[1]

4条回答
  •  抹茶落季
    2020-11-28 18:43

    Remember that the dot notation in a Django template is used for four different notations in Python. In a template, foo.bar can mean any of:

    foo[bar]       # dictionary lookup
    foo.bar        # attribute lookup
    foo.bar()      # method call
    foo[bar]       # list-index lookup
    

    It tries them in this order until it finds a match. So foo.3 will get you your list index because your object isn't a dict with 3 as a key, doesn't have an attribute named 3, and doesn't have a method named 3.

提交回复
热议问题