Cannot access python dictionnary keys in django template -
"simple" problem here solved in view... didn't find way it.
so here idea; have anonymous1, anymous2... anymous3 received calls , communications times on phone numbers. want each of them table :
number | calls | communication time 2-xxx-x 1 01:00:00 3-xxx-x 23 00:30:00 total 24 01:30:00
number of calls, communication time , total computed in view, has dedicated to. have list of dictionnaries contains numbers numbers of calls, communication time , owner. looking :
list = [{'number':2-xxx-x ,'owner':anonymous1' ,'calls':1 ,'communication time':datetime object},...]
why list of dictionnaries ? because using regroup template tags described in documentation: https://docs.djangoproject.com/fr/1.9/ref/templates/builtins/#regroup
i make dictionnary contains total number of calls, total communication time , owner; using compute sum of each column. here how looks :
second_dict = {'anonymous1':{'calls':24,'communication_time':datetime object}}
to access them, using loops in html code, have problem. create table, regrouping list of dictionnaries owner , performing loops , using dictionnary make th:
{% regroup list owner owner_list %} {% owner in owner_list %} <table class="display" cellspacing="0" style="position: relative; left: -250px;"> <caption> {{owner.grouper}}</caption> <thead> <tr> <th> {% trans "number" %} </th> <th> {% trans "calls"%}</th> <th> {% trans "communication time" %}</th> </tr> </thead> <tbody> {% item in owner.list%} <tr> <td>{{item.number}}</td> <td>{{item.calls}}</td> <td>{{item.communication_time}}</td> </tr> {% endfor %} <tr> {% owner.list.0.owner owner_name %} <td><b>{% trans "total" %}</b></td> <td>{{second_dict.owner_name.calls}} </td> <td> {{second_dict.owner_name.communication_time}} </td> {% endwith %} </tr> </tbody> </table> {% endfor %}
as can see code, want access second dictionary values owner key, following described here : https://docs.djangoproject.com/en/dev/ref/templates/api/ problem is... not working @ ! thinking str/unicode problem, moving 1 other when creating different dictionnaries in python views did not change anything.
anyone got hint on how solve ?
you cannot lookup in dictionary in template using variable, dict in template treat what's after dot string lookup second_dict['owner_name']
. need write template filter it. check django doc on how write custom filter.
Comments
Post a Comment