Django template object type

后端 未结 2 484
无人及你
无人及你 2021-02-07 12:40

Alright, here\'s my situation. I\'ve got an array of generic objects that I\'m iterating over in a django template. Those objects have a number of subclasses and I want to fig

2条回答
  •  抹茶落季
    2021-02-07 13:03

    This is an old question, but FWIW you can do this with a template filter.

    @register.filter
    def classname(obj):
        return obj.__class__.__name__
    

    Then in your template you can do:

    {% with beer|classname as modelclass %}
    {% if modelclass == "Domestic" %}US of A
    {% elif modelclass == "Import" %}Somewhere else
    {% endif %}
    {% endwith %}
    

提交回复
热议问题