I think that a simple template filter is best suited here. It is very fast to implement and easy to call. Something like that:
in templatetags / my_filters.py:
from django import template from django.utils.importlib import import_module register = template.Library() @register.filter def isinst(value, class_str): split = class_str.split('.') return isinstance(value, getattr(import_module('.'.join(split[:-1])), split[-1]))
in the template:
{% load my_filters %} ... {% if myvar|isinst:"mymodule.MyClass" %} ...do your stuff {% endif %}
Although the above sample code (not tested), I believe that it should work. For more information on custom template filters, see the django documentation
EDIT: Edited answer to show that the filter argument is actually a string, not a python class
source share