[Fixed]-How do you iterate tuple values in a dictionary for django context

0πŸ‘

βœ…

In your view:

titles = ('Hello', 'Umbrella')
artists = ('Adele', 'Rihanna')
songs = {'titles_artists': zip(titles, artist)}

in your template:

{% for title, artist in titles_artists %}
    <p>{{ title }}<br>{{ artist }}</p>
{% endfor %}

You can try rethink your context dictionary, something like:

songs = {'artists': [
                        {'name': 'Adele', 'titles': ['Hello', ]}, 
                        {'name': 'Rihanna', 'titles': ['Umbrella', ]}
                    ]
        }

And in your template:

{% for artist in artists %}
    {% for title in artist.titles %}
         {{ title }}
    {% endfor %}
    {{ artist.name }}
{% endfor %}
πŸ‘€Anderson Lima

1πŸ‘

Do this:

for title, artist in zip(songs['titles'], songs['artists']):
    print(title)
    print(artist)
    print() # In Python 2, remove the parentheses
πŸ‘€zondo

Leave a comment