django-formsaccessibilitydjango-crispy-forms

django forms add aria-label


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'


Solution

  • Add the attrs to the widget rather than the field.

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