sly*_*ete 6 python django django-templates
如何在将对象传递给模板之前对对象执行复杂排序?例如,这是我的观点:
@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,}))
Run Code Online (Sandbox Code Playgroud)
在模板中未正确排序医生对象.为什么不?
另外,如何索引模板中的列表?例如,(这不起作用):
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
谢谢一堆,皮特
Joh*_*ohn 11
正如其他人所指出的那样,您的两个问题最好在模板外部解决 - 无论是在模型中还是在视图中.一种策略是将辅助方法添加到相关类中.
获取医生患者的分类清单:
class Physician(Model):
...
def sorted_patients(self):
return self.patients.order_by('bed__room__unit',
'bed__room__order',
'bed__order')
Run Code Online (Sandbox Code Playgroud)
在模板中,使用physician.sorted_patients而不是physician.patients.
对于"显示此note_type的音符",听起来您可能想要一个notesnote_type类的方法.根据你的描述,我不确定这是否是一个模型类,但原理是相同的:
class NoteType:
...
def notes(self):
return <calculate note set>
Run Code Online (Sandbox Code Playgroud)
然后是模板:
{% for note_type in note_types %}
<div><h3>{{ note_type }}</h3></div>
{% for note in note_type.notes %}
<p>{{ note }}</p>
{% endfor %}
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)