Sorting and indexing into a list in a Django template?
Posted
by slypete
on Stack Overflow
See other posts from Stack Overflow
or by slypete
Published on 2009-07-28T01:07:41Z
Indexed on
2010/04/01
5:23 UTC
Read the original article
Hit count: 386
How can you perform complex sorting on an object before passing it to the template? For example, here is my view:
@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,}))
The physicians object is not ordered correctly in the template. Why not?
Additionally, how do you index into a list inside the template? For example, (this doesn't work):
{% 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 %}
© Stack Overflow or respective owner