简体   繁体   中英

Cannot access python dictionnary keys in django template

"Simple" problem here which would already be solved in a view... But I didn't find a way to do it.

So here is the idea; I have Anonymous1, Anymous2... Anymous3 who received calls and communications times on phone numbers. I want to do for each of them a table like that :

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 and total are all computed in the view, as it has to be dedicated to. I have a list of dictionnaries which contains all the numbers with the numbers of calls, the communication time and its owner. It is looking like that :

list = [{'number':2-xxx-x ,'owner':Anonymous1' ,'calls':1 ,'communication time':datetime object},...]

Why a list of dictionnaries ? Because I am using the regroup template tags as described in the documentation: https://docs.djangoproject.com/fr/1.9/ref/templates/builtins/#regroup

I also make a dictionnary which contains only the total number of calls, the total communication time and the owner; I am using it to compute the sum of each column. Here is it how it looks like :

second_dict = {'Anonymous1':{'calls':24,'communication_time':datetime object}}

To access them, I am using loops in my html code, which is where I have a problem. To create the table, I am regrouping the list of dictionnaries by their owner and performing loops and I am using the dictionnary to make th:

{% regroup list by owner as owner_list %}

{% for 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>
        {% for item in owner.list%}
        <tr>
            <td>{{item.number}}</td>
            <td>{{item.calls}}</td>
            <td>{{item.communication_time}}</td>   
        </tr>
        {% endfor %}
        <tr>
            {% with owner.list.0.owner as 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 you can see with the code, I want to access the second dictionary values with owner as a key, following what is described here : https://docs.djangoproject.com/en/dev/ref/templates/api/ The problem is... this is not working at all ! I was thinking it was a str/unicode problem, but moving from one to the other when creating the different dictionnaries in the python views did not change anything.

Anyone got an hint on how to solve this ?

You cannot do lookup in a dictionary in template using a variable, dict in template would always treat what's after the dot as a string lookup like second_dict['owner_name'] . You need to write a template filter to do it. Check django doc on how to write custom filter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM