Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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} )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading