Iterating though Dictionary values is not working for me in Django on GAE
Data is a sorted Dict from django.utils.datastructures import SortedDict
{'test1': '-10', 'quiz1': '65', 'quiz2': '40', 'quiz3': '55'}
This Template Code:
{% for key1 in Data %}
<td>key: {{ key1|capfirst }} - value: {{Data.key1}} - Data: {{ Data }}</td>
{% endfor %}
Yields:
key: Quiz1 - value: - Data: {'test1': '-10', 'quiz1': '65', 'quiz2': '40', 'quiz3': '55'}
key: Quiz2 - value: - Data: {'test1': '-10', 'quiz1': '65', 'quiz2': '40', 'quiz3': '55'}
key: Quiz3 - value: - Data: {'test1': '-10', 'quiz1': '65', 'quiz2': '40', 'quiz3': '55'}
key: Test1 - value: - Data: {'test1': '-10', 'quiz1': '65', 'quiz2': '40', 'quiz3': '55'}
I've tried the different examples in the Doc, but I'm stuck.
EDIT:
I tried this Code:
<table border="1">
<tr>
{% for key1 in Data %}
<td>{{ key1|capfirst }}</td>
{% endfor %}
</tr>
<tr>
{% for key2 in Data %}
<td>key: {{ key2|capfirst }}| Data: {{Data.key2}}| Static Keyed: {{ Data.quiz1 }}| AllData: {{ Data }}</td>
{% endfor 开发者_运维百科%}
</tr>
<tr>
{% for key,value in Data %}
<td>key: {{key}}: value: {{value}}</td>
{% endfor %}
</tr>
</table>
and this is the result:
<table border="1">
<tr>
<td>Quiz1</td>
<td>Quiz2</td>
<td>Quiz3</td>
<td>Test1</td>
</tr>
<tr>
<td>key: Quiz1| Data: | Static Keyed: 65| AllData: {'test1': '56', 'quiz1': '65', 'quiz2': '75', 'quiz3': '25'}</td>
<td>key: Quiz2| Data: | Static Keyed: 65| AllData: {'test1': '56', 'quiz1': '65', 'quiz2': '75', 'quiz3': '25'}</td>
<td>key: Quiz3| Data: | Static Keyed: 65| AllData: {'test1': '56', 'quiz1': '65', 'quiz2': '75', 'quiz3': '25'}</td>
<td>key: Test1| Data: | Static Keyed: 65| AllData: {'test1': '56', 'quiz1': '65', 'quiz2': '75', 'quiz3': '25'}</td>
</tr>
<tr>
<td>key: : value: </td>
<td>key: : value: </td>
<td>key: : value: </td>
<td>key: : value: </td>
</tr>
</table>
The Data is there, but django won't let me use a variable as a key. My problem is that I want the table to grow dynamically with a changing number of test/grade pairs passed to it. Is this normal?
EDIT:
It turns out that I am using a different version of django than I thought i was using. This code will do what I am trying to do in version 0.96 of django:
<tr>
{% for data in Data.items %}
<td>{{data.0}}: {{data.1}}</td>
{% endfor %}
</tr>
Thanks to everyone that helped, even though I initially gave the wrong info.
{% for key, value in Data.items %}
<td>key: {{ key|capfirst }} - value: {{ value }} - Data: {{ Data }}</td>
{% endfor %}
Try this . Previous solution will work only with django >1.0
{% for data in Data.items %}
<td>key: {{ data.0 }}: value: {{ data.1 }}</td>
{% endfor %}
If i'm not wrong, SortedDict inherits from Dict, i think you can use it in this way:
{% for key, value in Data %}
<td>key: {{ key|capfirst }} - value: {{value}}</td>
{% endofr %}
But i'm afraid is not clear What are the values in your dict.
精彩评论