mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #29200 -- Fixed label rendering when using RadioSelect and CheckboxSelectMultiple with MultiWidget.
This commit is contained in:
		| @@ -248,7 +248,7 @@ class BoundWidget: | |||||||
|         return self.tag(wrap_label=True) |         return self.tag(wrap_label=True) | ||||||
|  |  | ||||||
|     def tag(self, wrap_label=False): |     def tag(self, wrap_label=False): | ||||||
|         context = {'widget': self.data, 'wrap_label': wrap_label} |         context = {'widget': {**self.data, 'wrap_label': wrap_label}} | ||||||
|         return self.parent_widget._render(self.template_name, context, self.renderer) |         return self.parent_widget._render(self.template_name, context, self.renderer) | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| {% if wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if wrap_label %} {{ widget.label }}</label>{% endif %} | {% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %} {{ widget.label }}</label>{% endif %} | ||||||
|   | |||||||
| @@ -1 +1 @@ | |||||||
| {% if wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if wrap_label %} {{ widget.label }}</label>{% endif %} | {% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %} {{ widget.label }}</label>{% endif %} | ||||||
|   | |||||||
| @@ -621,12 +621,12 @@ class ChoiceWidget(Widget): | |||||||
|             'attrs': option_attrs, |             'attrs': option_attrs, | ||||||
|             'type': self.input_type, |             'type': self.input_type, | ||||||
|             'template_name': self.option_template_name, |             'template_name': self.option_template_name, | ||||||
|  |             'wrap_label': True, | ||||||
|         } |         } | ||||||
|  |  | ||||||
|     def get_context(self, name, value, attrs): |     def get_context(self, name, value, attrs): | ||||||
|         context = super().get_context(name, value, attrs) |         context = super().get_context(name, value, attrs) | ||||||
|         context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs) |         context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs) | ||||||
|         context['wrap_label'] = True |  | ||||||
|         return context |         return context | ||||||
|  |  | ||||||
|     def id_for_label(self, id_, index='0'): |     def id_for_label(self, id_, index='0'): | ||||||
|   | |||||||
| @@ -310,6 +310,15 @@ If you want to continue to allow those passwords to be used, you'll | |||||||
| have to define the :setting:`PASSWORD_HASHERS` setting (if you don't already) | have to define the :setting:`PASSWORD_HASHERS` setting (if you don't already) | ||||||
| and include ``'django.contrib.auth.hashers.BCryptPasswordHasher'``. | and include ``'django.contrib.auth.hashers.BCryptPasswordHasher'``. | ||||||
|  |  | ||||||
|  | Moved ``wrap_label`` widget template context variable | ||||||
|  | ----------------------------------------------------- | ||||||
|  |  | ||||||
|  | To fix the lack of ``<label>`` when using ``RadioSelect`` and | ||||||
|  | ``CheckboxSelectMultiple`` with ``MultiWidget``, the ``wrap_label`` context | ||||||
|  | variable now appears as an attribute of each option. For example, in a custom | ||||||
|  | ``input_option.html`` template, change ``{% if wrap_label %}`` to | ||||||
|  | ``{% if widget.wrap_label %}``. | ||||||
|  |  | ||||||
| Miscellaneous | Miscellaneous | ||||||
| ------------- | ------------- | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| import datetime | import datetime | ||||||
|  |  | ||||||
| from django.forms import RadioSelect | from django.forms import MultiWidget, RadioSelect | ||||||
| from django.test import override_settings | from django.test import override_settings | ||||||
|  |  | ||||||
| from .base import WidgetTest | from .base import WidgetTest | ||||||
| @@ -130,3 +130,16 @@ class RadioSelectTest(WidgetTest): | |||||||
|         </ul> |         </ul> | ||||||
|         """ |         """ | ||||||
|         self.check_html(self.widget(choices=choices), 'time', None, html=html) |         self.check_html(self.widget(choices=choices), 'time', None, html=html) | ||||||
|  |  | ||||||
|  |     def test_render_as_subwidget(self): | ||||||
|  |         """A RadioSelect as a subwidget of MultiWidget.""" | ||||||
|  |         choices = (('', '------'),) + self.beatles | ||||||
|  |         self.check_html(MultiWidget([self.widget(choices=choices)]), 'beatle', ['J'], html=( | ||||||
|  |             """<ul> | ||||||
|  |             <li><label><input type="radio" name="beatle_0" value=""> ------</label></li> | ||||||
|  |             <li><label><input checked type="radio" name="beatle_0" value="J"> John</label></li> | ||||||
|  |             <li><label><input type="radio" name="beatle_0" value="P"> Paul</label></li> | ||||||
|  |             <li><label><input type="radio" name="beatle_0" value="G"> George</label></li> | ||||||
|  |             <li><label><input type="radio" name="beatle_0" value="R"> Ringo</label></li> | ||||||
|  |             </ul>""" | ||||||
|  |         )) | ||||||
|   | |||||||
| @@ -294,6 +294,7 @@ class SelectTest(WidgetTest): | |||||||
|                 'template_name': 'django/forms/widgets/select_option.html', |                 'template_name': 'django/forms/widgets/select_option.html', | ||||||
|                 'name': 'name', |                 'name': 'name', | ||||||
|                 'selected': False, |                 'selected': False, | ||||||
|  |                 'wrap_label': True, | ||||||
|             }, { |             }, { | ||||||
|                 'value': 'cd', |                 'value': 'cd', | ||||||
|                 'type': 'select', |                 'type': 'select', | ||||||
| @@ -303,6 +304,7 @@ class SelectTest(WidgetTest): | |||||||
|                 'template_name': 'django/forms/widgets/select_option.html', |                 'template_name': 'django/forms/widgets/select_option.html', | ||||||
|                 'name': 'name', |                 'name': 'name', | ||||||
|                 'selected': False, |                 'selected': False, | ||||||
|  |                 'wrap_label': True, | ||||||
|             }] |             }] | ||||||
|         ) |         ) | ||||||
|         self.assertEqual(index, 0) |         self.assertEqual(index, 0) | ||||||
| @@ -319,6 +321,7 @@ class SelectTest(WidgetTest): | |||||||
|                 'name': 'name', |                 'name': 'name', | ||||||
|                 'selected': True, |                 'selected': True, | ||||||
|                 'type': 'select', |                 'type': 'select', | ||||||
|  |                 'wrap_label': True, | ||||||
|             }, { |             }, { | ||||||
|                 'value': 'dvd', |                 'value': 'dvd', | ||||||
|                 'template_name': 'django/forms/widgets/select_option.html', |                 'template_name': 'django/forms/widgets/select_option.html', | ||||||
| @@ -328,6 +331,7 @@ class SelectTest(WidgetTest): | |||||||
|                 'name': 'name', |                 'name': 'name', | ||||||
|                 'selected': False, |                 'selected': False, | ||||||
|                 'type': 'select', |                 'type': 'select', | ||||||
|  |                 'wrap_label': True, | ||||||
|             }] |             }] | ||||||
|         ) |         ) | ||||||
|         self.assertEqual(index, 1) |         self.assertEqual(index, 1) | ||||||
| @@ -344,6 +348,7 @@ class SelectTest(WidgetTest): | |||||||
|                 'index': '2', |                 'index': '2', | ||||||
|                 'name': 'name', |                 'name': 'name', | ||||||
|                 'type': 'select', |                 'type': 'select', | ||||||
|  |                 'wrap_label': True, | ||||||
|             }] |             }] | ||||||
|         ) |         ) | ||||||
|         self.assertEqual(index, 2) |         self.assertEqual(index, 2) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user