简体   繁体   中英

Pass nested dictionary from views to template django

So, I'm new to Django and I'm trying to pass data from my view to a template. I've understood how to pass the classic dictionary but I now need to use a nested dictionary.

For instance, I have a dictionary as below

my_dictionary = {0: {'title': 'Beyond the Soul', 'id': '2Uy66My5oKcBEIW7DvVk3V'},
1: {'title': 'The Groove Cartel Selection', 'id': '1pHtICGI68RmWEKnnP5wGr'},
2: {'title':
 'STMPD RCRDS TOP 50', 'id': '1OIzwJTbrOeZTHvUXf5yMg'},
3: {'title': 'House Party by Axwell', 'id': '1tl4L77FJju5zp9bJC83u8'}}

From my view I'm returning return render(request, 'show_playlist.html', my_dictionary ) but If I use {{ main_playlist[0] }} it gives Could not parse the remainder error

Is there a way to access nested dictionary in templates?

I've tried from this answer bu doesn't show anything

{% for key, value in main_playlist.items %}
    <p> {{key}}: {{value.title}} </p>
    {% endfor %}

The context data has to be named properly if you want to use main_playlist on the template so:

return render(request, 'show_playlist.html', {"main_playlist": my_dictionary} )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM