from django.template.defaulttags import register @register.filter def get(o, key): if hasattr(o, 'get'): return o.get(key) return o[int(key)]