30👍
✅
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 %}
10👍
You’ll have to do it via some sort of method. Why not just write a method like display_location()
or something on the model itself and have it return the string which gets rendered there? Then you could just put {{ beer.display_location }}
in your template.
Or if you want to go really crazy, write a custom template tag that does what you want, but that’s much more work.
Source:stackexchange.com