import re from django import template from django.utils.safestring import mark_safe register = template.Library() shorttag_regex = re.compile('^<([^/>]*[^\s]+)\s*/>$') @template.defaultfilters.stringfilter def remove_shorttag(field): search = shorttag_regex.search(field) if search: try: return mark_safe(u'<%s>' % search.groups()[0]) except: return mark_safe(field) else: return mark_safe(field) remove_shorttag.is_safe = True register.filter('remove_shorttag', remove_shorttag)