python - Django Template Tag Loop dictionary variable -
i've reading template tags posts regarding loop variable in key. apparently django not support loop variable in key , not sure how use custom template tag.
i wanted display this, how can achieve {% in mdata %} loop ?
{{ mdata.0.name }} {{ mdata.1.name }} {{ mdata.2.name }} {{ mdata.0.age }} {{ mdata.1.age }} {{ mdata.2.age }}
mdata list of dictionaries.
mdata = { "name":"alex", "age":"12"},{"name":"amy","age":"14"} ...
considering data in list of dictionaries such as:
my_data = [{"name" : "abc" , "age":20,...}, {} , {}...]
you can access attributes of each dictionary in template way:
{% dict in my_data %} <!-- here dict each of dictionary in my_data --> <!-- can access elements of dict using dot syntax, dict.property --> {{ dict.name }}, {{ dict.age }}, ... {{ dict.property }} {% endfor %}
reference links: django templating language
if want structure elements in order specifed, can this:
name list: {% dict in my_data %} my_data.name {% endfor %} age list: {% dict in my_data %} my_data.age {% endfor %} ... prpoerty list: {% dict in my_data %} my_data.property {% endfor %}
Comments
Post a Comment