对Django模板中的列表进行排序和索引?

时间:2022-05-26 22:42:47

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

谢谢一堆,皮特

4 个解决方案

#1


11  

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_pa​​tients而不是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的音符”,听起来您可能需要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 %}

#2


4  

"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.

应该足够了。将结果提供给模板以进行渲染。这是正确的顺序。

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

如果模板看起来太复杂了,那就是。保持模板简单 - 它只是演示文稿。处理进入视图功能

#3


1  

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?

然后,您的模板可以循环遍历包含有序结果的患者。这不适合你吗?

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-context for more information.

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

#4


0  

This is one way of doing it, although very ugly :

这是一种做法,虽然非常难看:

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

{%for note_sets | slice:“forloop.counter0”| first%}

#1


11  

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_pa​​tients而不是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的音符”,听起来您可能需要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 %}

#2


4  

"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.

应该足够了。将结果提供给模板以进行渲染。这是正确的顺序。

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

如果模板看起来太复杂了,那就是。保持模板简单 - 它只是演示文稿。处理进入视图功能

#3


1  

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?

然后,您的模板可以循环遍历包含有序结果的患者。这不适合你吗?

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-context for more information.

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

#4


0  

This is one way of doing it, although very ugly :

这是一种做法,虽然非常难看:

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

{%for note_sets | slice:“forloop.counter0”| first%}