python 在 Django 模板中对列表进行排序和索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1191439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting and indexing into a list in a Django template?
提问by slypete
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 %}
Thanks a bunch, Pete
非常感谢,皮特
回答by John
As others have indicated, both of your problems are best solved outside the template -- either in the models, or in the view. One strategy would be to add helper methods to the relevant classes.
正如其他人所指出的,您的两个问题最好在模板之外解决——无论是在模型中,还是在视图中。一种策略是向相关类添加辅助方法。
Getting a sorted list of a physician's patients:
获取医生患者的排序列表:
class Physician(Model):
...
def sorted_patients(self):
return self.patients.order_by('bed__room__unit',
'bed__room__order',
'bed__order')
And in the template, use physician.sorted_patients
rather than physician.patients
.
在模板中,使用physician.sorted_patients
而不是physician.patients
.
For the "display the notes of this note_type", it sounds like you might want a notes
method for the note_type class. From your description I'm not sure if this is a model class or not, but the principle is the same:
对于“显示此 note_type 的笔记”,听起来您可能需要一个notes
note_type 类的方法。根据您的描述,我不确定这是否是模型类,但原理是相同的:
class NoteType:
...
def notes(self):
return <calculate note set>
And then the template:
然后是模板:
{% for note_type in note_types %}
<div><h3>{{ note_type }}</h3></div>
{% for note in note_type.notes %}
<p>{{ note }}</p>
{% endfor %}
</div>
{% endfor %}
回答by S.Lott
"I'd like to do this from within a template:"
“我想从模板中执行此操作:”
Don't. Do it in the view function where it belongs.
别。在它所属的视图函数中进行。
Since the question is incomplete, it's impossible to guess at the data model and provide the exact solution.
由于问题不完整,因此无法猜测数据模型并提供确切的解决方案。
results= physician.patients.order_by('bed__room__unit', 'bed__room__order', 'bed__order')
Should be sufficient. Provide results
to the template for rendering. It's in the proper order.
应该够了。提供results
给模板进行渲染。它的顺序正确。
If this isn't sorting properly (perhaps because of some model subtletly) then you always have this kind of alternative.
如果这没有正确排序(可能是因为某些模型巧妙地),那么您总是有这种选择。
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 )
Provide patient_list
to the template for rendering. It's in the proper order.
提供patient_list
给模板进行渲染。它的顺序正确。
"how do you index into a list inside the template"
“你如何索引到模板内的列表中”
I'm not sure what you're trying to do, but most of the time, the answer is "Don't". Do it in the view function.
我不确定您要做什么,但大多数情况下,答案是“不要”。在视图函数中进行。
The template just iterate through simple lists filling in simple HTML templates.
该模板只是遍历填充简单 HTML 模板的简单列表。
If it seems too complex for a template, it is. Keep the template simple -- it's only presentation. The processing goes in the view function
如果模板看起来太复杂,那就是。保持模板简单——它只是演示文稿。处理在视图函数中进行
回答by ars
You should be able to construct the ordered query set in your view and pass it to your template:
您应该能够在您的视图中构建有序查询集并将其传递给您的模板:
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')
Your template can then loop over patients
which will contain the ordered results. Does this not work for you?
然后您的模板可以循环,patients
其中将包含有序结果。这不适合你吗?
EDIT: For indexing, just use the dot syntax: mylist.3
in a template becomes mylist[3]
in python. See http://docs.djangoproject.com/en/dev/ref/templates/api/#rendering-a-contextfor more information.
编辑:对于索引,只需使用点语法: mylist.3
在模板中变成mylist[3]
在 python 中。有关 更多信息,请参阅http://docs.djangoproject.com/en/dev/ref/templates/api/#rendering-a-context。
回答by ars
This is one way of doing it, although very ugly :
这是一种方法,虽然很丑陋:
{% for note in note_sets|slice:"forloop.counter0"|first %}
{% for note in note_sets|slice:"forloop.counter0"|first %}