Home > Mobile >  Pass nested dictionary from views to template django
Pass nested dictionary from views to template django

Time:02-15

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 %}

CodePudding user response:

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} )
  • Related