@register.filter
def field_attrs(field_inst, **kwargs):
    for k, v in kwargs.items():
        if v is not None:
            field_inst.field.widget.attrs[k] = v
        else:
            try:
                del field_inst.field.widget.attrs[k]
            except KeyError:
                pass
    return field_inst
Adds html attributes to django form fields
dataset/ETHPy150Open jbalogh/jingo/jingo/helpers.py/field_attrs