I am passing a list to a view. My array looks like this: [football, basketball, soccer]. In my view, I would like to display something like this:
1. football
2. basketball
3. soccer
This means that I would have to iterate through the list that is passed to the array, do a for loop on the elements. How would I do this in the template file?
For each element, I would have to get its index and add 1 to it. Now, I have no idea how to do this in the views. One way I thought about doing this would be to make a dictionary, like so, {'1', 'football': '2', 'basketball': '3', 'soccer'}, but since I already have the data in a list format, I would rather not convert it.
You want the forloop.counter template variable.
So your code would look like:
Of course, for simply displaying the data as you do in your question, the easier way is just to show the items in an ordered list, and the browser will do the numbering for you: