django forms add aria-label

68 Views Asked by At

How does one add an aria-label to a django form field? Is there a standard way?

Can I add it to the field definition or as part of the crispy layout a la css_class=""?

I tried adding it via an ```attrs``, e.g.

range_start = forms.DateField(
        required=False,
        widget=DateInput(),
        attrs={'aria-label': 'whatevs'}
    )

but that gave a TypeError: TypeError: Field.__init__() got an unexpected keyword argument 'attrs'

1

There are 1 best solutions below

0
David Smith On BEST ANSWER

Add the attrs to the widget rather than the field.

range_start = forms.DateField(
        required=False,
        widget=DateInput(attrs={'aria-label': 'whatevs'}),
    )