Django aggregation in templates?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:34:44

This work should be done in the view. You say that feels wrong but that's exactly what the view is meant to do: retrieve the data that is going to be presented to the user. Take a look at the Django FAQ: http://docs.djangoproject.com/en/dev/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names

Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”. How come you don’t use the standard names?

In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

A side point to this is that based on how you are using the average in your template it would probably make more sense to use annotate rather than aggregate. http://docs.djangoproject.com/en/1.1/ref/models/querysets/#annotate-args-kwargs

The templates do not let you call functions that take arguments. See here for what they let you do.

The canonical way to deal with this would be to either add some helper methods/managers to your models or to create a custom template tag or filter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!