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

后端 未结 4 585
夕颜
夕颜 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:27

    a bit simpler; assuming your layout is a list of a single model:

    class ObjectListView(ListView):
        model = Person
        template_name = 'object_list.html'
    
        def model_name(self):
            return self.model._meta.verbose_name
    

    Then in object_list.html:

    {% for obj in object_list %}
        
    ...
    {% endfor }}

提交回复
热议问题