Django 4.1.1 ChoiceField widget=forms.RadioSelect label tag missing 'for' attribute

142 views Asked by At

I just upgraded from django 2.0.2 to 4.1.1. I have a few form fields of type ChoiceField with forms.RadioSelect. In django 2.0.2 the tag included a 'for' attribute/id so I can query in the template {% if field.id_for_label == "xyz" %}. However in 4.1.1 the 'for' attribute/id is not included (when inspecting the html source generated), so field.id_for_label is empty.

All other fields labels still include 'for' attributes/id.

Questions:

  • Has anyone seen this problem?
  • Is there a way to 'force' a 'for' attribute on the labels?
  • Is there some other way to identify a field? I tried ... {% if field.label_tag == "label text" %}, but no good.
1

There are 1 answers

2
Maxim Danilov On

@doug-nintzel. Probably i initiated this change in last version of Django.

If i take the radio-widget template code, here:

django\forms\templates\django\forms\widgets\radio.html
django\forms\templates\django\forms\widgets\radio-option.html

i see, you can use:

{% if field.widget.attrs.id == "something" %} ... {% endif %}

instead of field.id_for_label