我决定使用字典而不是列表来获取每个季度的成绩。我能够为我的dict中的每个键添加值,但是我很难访问这些值。
到目前为止我的dict代码:
for sg in x.subjectgrade_set.all():
...: if sg.status == 'approved':
...: for component in sg.subject_schedule.component_set.all():
...: if component.is_root:
...: try:
...: grade = sg.grade_set.get(component=component)
...: if component.name == '1st Quarter':
...: y[sg.subject.name] = [grade.grade]
...: elif component.name == '2nd Quarter':
...: y[sg.subject.name] = [y[sg.subject.name], grade.grade]
...: elif component.name == '3rd Quarter':
...: y[sg.subject.name] = [y[sg.subject.name], grade.grade]
...: elif component.name == '4th Quarter':
...: y[sg.subject.name] = [y[sg.subject.name], grade.grade]
...: except:
...: pass
输出
{u's1': [[[[Decimal('72.00')], Decimal('74.00')], Decimal('79.00')],
Decimal('83.00')],
u's2': [[[[Decimal('60.00')], Decimal('60.00')], Decimal('60.00')],
Decimal('60.00')],
u's3': [[[[Decimal('64.00')], Decimal('64.00')], Decimal('64.00')],
Decimal('64.00')],
u's4': [[[[Decimal('93.00')], Decimal('93.00')], Decimal('93.00')],
Decimal('93.00')],
u's5': [[[[Decimal('64.00')], Decimal('62.00')], Decimal('64.00')],
Decimal('65.00')],
u's6': [[[[Decimal('75.00')],
Decimal('75.00')],
Decimal('75.00')],
Decimal('75.00')]}
我尝试将每个季度的每个成绩放在一个列表中,然后将其分配给我现有的密钥,然后在我的html中显示它们。如果我尝试运行下面的代码,如果给我从一级评分到三级评分的等级以及主题名称的第一个字符:
for i in y.iteritems():
...: for o in i:
...: print o[0]
这样做的最佳方法是什么?
TIA
答案 0 :(得分:0)
通过使用shell_plus
中的代码来解决问题 {% for key, val in grades_dict.iteritems %}
<tr>
<td>{{key}}</td>
<td>{{val.0.0.0.0.0}}</td>
<td>{{val.0.0.0.1}}</td>
<td>{{val.0.0.1}}</td>
<td>{{val.0.1}}</td>
<td>{{val.1}}</td>
</tr>
{% endfor %}