Django:模板中显示的多维词典 [英] Django: multi-dimensional dictionary displayed in template

查看:47
本文介绍了Django:模板中显示的多维词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下视图,发现无法通过模板进行显示:

I have the following view and I'm finding it impossible to display through a template:

    sm = request.session.get('active_semester')

    semester_dates = SemesterDates.objects.filter(semester=sm)
    schedule = Schedule.objects.filter(semester=sm)
    student_enrolls = StudentEnroll.objects.filter(schedule__semester = sm).values_list('schedule__pk', 'student__family_member__first_name', 'student__family_member__last_name').order_by('student__family_member__last_name')
    schedule_array={}
    for a in schedule:
        schedule_array[a.id]={'course':a.course_catalog.course_name, 'students': [], 'teachers': []}

    for (pk, fname, lname) in student_enrolls:
        schedule_array[pk]['students'].append(fname + ' ' + lame)

这将产生以下输出.请注意,有两个"Arduino"类,因为它们在不同的时间举行.因此,我使用Schedule.pk作为字典键来使它们分开.这样,本课程的每个实例的班级列表都会有所不同.

This produces the following output. Note that there are two "Arduino" classes because they are held at different times. So, I'm using the Schedule.pk as the dictionary key to keep them separate. This way my class list is different for each instance of this course.

{2L: 
   {
    'students': [u'Jessica Ryan', u'Annie Ryan'], 
    'course': u'Arduino Programming', 
    'teachers': []
   }, 
4L: 
   {
    'students': [], 
    'course': u'Lego Animation 3rd-4th Grade', 
    'teachers': []
   }, 
5L: 
   {
    'students': [], 
    'course': u'Life skills - card games', 
    'teachers': []
   }, 
6L: 
   {
    'students': [], 
    'course': u'test', 
    'teachers': []
   }, 
7L: {
    'students': [u'Mark Ryan'], 
    'course': u'Arduino Programming', 
    'teachers': []
    }
}

在我的模板中,我有以下内容.这是行不通的,但是让您了解我正在尝试做的事情.

In my template, I have the following. This is not working but gives you a glimpse of what I'm trying to do.

    <table class="table table-hover">
    {% for e in schedule %}
        <tr class="info">
          <td><h3>{{ e.course_catalog }}</h3></td>
        </tr>
        <tr>
          <td>
            {% for key in schedule_array.items|lookup:0 %}
              <h5>Dict Key: {{ key }} & Value: {{ value }}  {{e.pk}} </h5>
              {% for k2 in key.items %}
                k2: {{k2}} Students: {{k2.students}}<br>
                {% for k3 in k2 %}
                    {% for s in k3 %}
                      k3:{{s.students}}
                    {% endfor %}
                {% endfor %}
              endfor 2
              {% endfor %}
            endfor 1
            {% endfor %}
          </td>
        </tr>
    {% endfor %}
    </table>

我想做的是遍历我的日程表对象.然后,我想在schedule_array字典中查找主键(e.pk).因此,我想遍历学生名单,然后与老师再次讨论.我无法使它正常工作.不管我尝试什么.我知道我做错了,但是找不到正确的方法来完成它.

What I'm trying to do is loop through my schedule object. Then, I'd like to look up the primary key (e.pk) in the schedule_array dictionary. From that, I'd like to loop through the list of students and then do it again with the teachers. I'm not able to get this to work. Regardless of what I try. I know I'm doing it wrong but can't find the correct way to accomplish it.

推荐答案

弄清楚了:

<table class="table table-hover">
  {% for key, value in schedule_array.items %}
    <tr class="info">
      <td><h3>{{ value.course }}</h3></td>
    </tr>
    <tr>
      <td>
        {% for s in value.students %}
            k2: {{s}}
          {% endfor %}
      </td>
      <td>
        {% for t in value.teachers %}
            k2: {{t}}
          {% endfor %}
      </td>
    </tr>
{% endfor %}
</table>

这篇关于Django:模板中显示的多维词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆