Wednesday, 15 September 2010

python - Django1.4: How to use order_by in template? -



python - Django1.4: How to use order_by in template? -

django1.4: how utilize order_by in template?

models.py

from django.db import models django.contrib.auth.models import user django.contrib.contenttypes.models import contenttype django.contrib.contenttypes import generic class note(models.model): contents = models.textfield() author = models.foreignkey(user, to_field='username') date = models.datetimefield(auto_now_add=true) content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield() content_object = generic.genericforeignkey('content_type', 'object_id') class customer(models.model): name = models.charfield(max_length=50,) notes = generic.genericrelation(note, null=true)

above models.py.

i want utilize 'order_by'(https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by)

and...

views.py

from django.views.generic import detailview crm.models import * class customerdetailview(detailview): context_object_name = 'customerdetail' template_name = "customerdetail.html" allow_empty = true model = client slug_field = 'name'

my views.py utilize detailview(https://docs.djangoproject.com/en/1.4/ref/class-based-views/#detailview).

and

customerdetail.html

<table class="table table-bordered" style="width: 100%;"> <tr> <td>note</td> </tr> {% in customerdetail.notes.all.order_by %}<!-- it's not working --> <tr> <th>({{ i.date }}) {{ i.contents }}[{{ i.writer }}]</th> </tr> {% endfor %} </table>

i want utilize order_by in template...

what should do?

the order_by needs @ to the lowest degree 1 argument, , django not allow pass arguments function or method within template.

some alternatives are:

use jinja2 template engine instead of django's 1 (jinja2 allow pass arguments methods , said perform better) order dataset in view use "meta:ordering" attribute define default ordering criteria models write custom filter can queryset|order_by:'somefield' (see snippet) as suggested michal, can write custom manager predefined methods orderings need

python django django-models python-2.7 django-1.4

No comments:

Post a Comment