[Answered ]-Data structure python django

2👍

you could keep games as a list but use dictionaries for each element inside your the list, this will allow you to keep the key/value structure for each game:

games = []
games.append({'time':'12:00', 'hometeam': 'Manchester United', 'awayteam': 'Chelsea'})
games.append({'time':'15:00', 'hometeam': 'Manchester United', 'awayteam': 'Chelsea'})

and will also let you loop through the list and/or refer to a game by its index.

So for example using dot notation in a django template to access the first game’s time would look like this:

{{games.0.time}}

Here is more about dictionaries in the python documentation: https://docs.python.org/2/tutorial/datastructures.html#dictionaries

And here is bit in Django documentation that explains dot notation in templates https://docs.djangoproject.com/en/dev/ref/templates/api/#variables-and-lookups

👤brobas

Leave a comment