from django import template register = template.Library() @register.tag def annotate_form_field(parser, token): """ Set an attribute on a form field with the widget type This means templates can use the widget type to render things differently if they want to. Django doesn't make this available by default. """ args = token.split_contents() if len(args) < 2: raise template.TemplateSyntaxError( "annotate_form_field tag requires a form field to be passed") return FormFieldNode(args[1]) class FormFieldNode(template.Node): def __init__(self, field_str): self.field = template.Variable(field_str) def render(self, context): field = self.field.resolve(context) field.widget_type = field.field.widget.__class__.__name__ return ''