tags:

views:

60

answers:

1

If I have a list of objects that require similar layouts but need some attribute set based on the class of object how can I go about getting the class name while class and other xx values are not available within templates.

{% for obj in objects %}
 <div class={{obj.__class__.__name__}}
   ..
 </div>
{% endfor }}

There is probably an alternative approach i'm missing here..

+1  A: 

A little dirty solution

If objects is a QuerySet that belong to a model, you can add a custom method to your model.

 class mymodel(models.Model):
     foo = models........


 def get_cname(self):
    class_name = ....
    return class_name

then in your template you can try:

{% for obj in objects %}
   <div class="{{obj.get_cname}}">
     ..
  </div>
{% endfor }}
Andrea Di Persio
yup, thanks - that's exactly what i've come up with.