기본 콘텐츠로 건너뛰기

[django] How to use order_by in template?

source: http://stackoverflow.com/questions/14856097/django1-4-how-to-use-order-by-in-template

question:
How to use order_by in template?

answer:

The order_by needs at least one argument, and Django does not allow you to pass arguments to a function or method inside a template.
Some alternatives are:
  • Use Jinja2 template engine instead of Django's one (Jinja2 will let you pass arguments to methods and is said to perform better)
  • order the dataset in the view
  • use the "Meta:ordering" attribute to define a default ordering criteria for your models
  • write a custom filter so you can queryset|order_by:'somefield' (see this snippet)
  • as suggested by Michal, you can write a custom Manager with predefined methods for the orderings you need

댓글