代码之家  ›  专栏  ›  技术社区  ›  slypete

在django模板中对列表进行排序和索引?

  •  5
  • slypete  · 技术社区  · 16 年前

    在将对象传递到模板之前,如何对其执行复杂排序?例如,这里是我的视图:

    @login_required
    def overview(request):
       physicians = PhysicianGroup.objects.get(pk=physician_group).physicians
    
    for physician in physicians.all():
        physician.service_patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
    
    return render_to_response('hospitalists/overview.html', RequestContext(request,  {'physicians': physicians,}))
    

    模板中医师对象的顺序不正确。为什么不呢?

    此外,如何索引到模板内的列表中?例如,(这不起作用):

    {% for note_type in note_types %}
       <div><h3>{{ note_type }}</h3>
       {% for notes in note_sets.index(parent.forloop.counter0) %}
       #only want to display the notes of this note_type!
          {% for note in notes %}
             <p>{{ note }}</p>
          {% endfor %}
       {% endfor %}
       </div>
    {% endfor %}
    

    谢谢你,皮特

    4 回复  |  直到 15 年前
        1
  •  11
  •   John    16 年前

    正如其他人所指出的,您的两个问题最好在模板之外解决——在模型中或视图中。一种策略是向相关类添加助手方法。

    获取医生的患者的分类列表:

    class Physician(Model):
       ...
       def sorted_patients(self):
          return self.patients.order_by('bed__room__unit',
                                        'bed__room__order',
                                        'bed__order')
    

    在模板中,使用 physician.sorted_patients 而不是 physician.patients .

    对于“显示此便笺类型的便笺”,听起来您可能需要 notes note_类型类的方法。根据你的描述,我不确定这是不是一个模型类,但原理是一样的:

    class NoteType:
       ...
       def notes(self):
          return <calculate note set>
    

    然后模板:

    {% for note_type in note_types %}
       <div><h3>{{ note_type }}</h3></div>
       {% for note in note_type.notes %}
          <p>{{ note }}</p>
       {% endfor %}
       </div>
    {% endfor %}
    
        2
  •  4
  •   S.Lott    16 年前

    “我想在模板内执行此操作:”

    不要。在它所属的视图函数中执行。

    由于问题是不完整的,所以不可能猜测数据模型并提供准确的解决方案。

    results= physician.patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
    

    应该足够了。提供 results 到模板进行渲染。一切都井然有序。

    如果这没有正确排序(可能是因为某些模型的细微差别),那么您总是有这种选择。

    def by_unit_room_bed( patient ):
        return patient.bed.room.unit, patient.bed.room.order, patient.bed.order
    
    patient_list = list( physician.patients )
    patient_list.sort( key=by_unit_room_bed )
    

    提供 patient_list 到模板进行渲染。一切都井然有序。

    “如何索引到模板内的列表中”

    我不确定你想做什么,但大多数时候,答案是“不要”。在查看功能中执行此操作。

    这个模板只是迭代简单的列表来填充简单的HTML模板。

    如果它对于模板来说太复杂,那么它就是。保持模板简单——它只是一个表示。在视图函数中进行处理

        3
  •  1
  •   ars    16 年前

    您应该能够在视图中构造有序的查询集并将其传递给模板:

    def myview(request):
        patients = Physician.patients.order_by('bed__room__unit', 
                                               'bed__room__order', 
                                               'bed__order')
        return render_to_response('some_template.html',
                                  dict(patients=patients), 
                                  mimetype='text/html')
    

    然后您的模板可以循环 patients 它将包含有序的结果。这对你不管用吗?

    编辑:对于索引,只需使用点语法: mylist.3 在模板中变成 mylist[3] 在巨蟒中。参见 http://docs.djangoproject.com/en/dev/ref/templates/api/#rendering-a-context 更多信息。

        4
  •  0
  •   user306579    15 年前

    这是一种方法,尽管非常难看:

    {% for note in note_sets|slice:"forloop.counter0"|first %}