How do I get the class of a object within a Django template?

后端 未结 4 589
夕颜
夕颜 2021-02-12 16:58

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

4条回答
  •  不思量自难忘°
    2021-02-12 17:17

    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 %}
       
    ..
    {% endfor }}

提交回复
热议问题