List of dictionaries - How to render in mako

385 views
Skip to first unread message

Javi D R

unread,
Jun 16, 2020, 11:39:46 AM6/16/20
to Mako Templates for Python
Hi

I am starting to work with Mako and im finding some problems. Apologies if the question is dumb

I am finding two problems. The first one, is that i can't send a list of data to mako to render it in an HTML. 

I have a list of dictionaries like this:

[{'name' : 'javi', 'country' : 'Spain'}, {'name' : 'john', 'country' : 'UK'}]


And i want to create an HTML like this:

javi - Spain
john - UK

What is the best way of doing this with Mako? As far as i know, you can't send a list as a parameter to render method in Mako, isnt it?


The second one comes after i tried to created the list as a parameter in the dictionary, something like this:

{'data' :[{'name' : 'javi', 'country' : 'Spain'}, {'name' : 'john', 'country' : 'UK'}]}

My template is as follows:

%for monster in monsters:
<H2>${monster.name}</H2>
%endfor

But then, I have this error:

AttributeError: 'dict' object has no attribute 'name'


What is the right way to access a key inside a dictionary?

Thanks

Kostiantyn Ivashchenko

unread,
Jun 16, 2020, 3:30:05 PM6/16/20
to mako-d...@googlegroups.com
Hi, 
in first case try this way:
% for d in list_dict:
<p>${d['name']} - ${d['country']}</p>
% endfor

in second:
% for d in compl_list_dict:
<p>${d['data'][0]['name']} - ${d['data'][1]['country']}</p>
% endfor

вт, 16 черв. 2020, 18:39 користувач Javi D R <javi.x...@gmail.com> пише:
--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mako-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mako-discuss/e195b0a4-adfa-455e-8ca5-2a84db2f281bo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages