Kaydet (Commit) 0034e9af authored tarafından Mariusz Felisiak's avatar Mariusz Felisiak Kaydeden (comit) Tim Graham

Fixed #5851 -- Allowed specifying different HTML attrs for SplitDateTimeWidget subwidgets.

Thanks Tim Graham and Nick Pope for review.
üst 3a148f95
...@@ -855,12 +855,18 @@ class SplitDateTimeWidget(MultiWidget): ...@@ -855,12 +855,18 @@ class SplitDateTimeWidget(MultiWidget):
supports_microseconds = False supports_microseconds = False
template_name = 'django/forms/widgets/splitdatetime.html' template_name = 'django/forms/widgets/splitdatetime.html'
def __init__(self, attrs=None, date_format=None, time_format=None): def __init__(self, attrs=None, date_format=None, time_format=None, date_attrs=None, time_attrs=None):
widgets = ( widgets = (
DateInput(attrs=attrs, format=date_format), DateInput(
TimeInput(attrs=attrs, format=time_format), attrs=attrs if date_attrs is None else date_attrs,
format=date_format,
),
TimeInput(
attrs=attrs if time_attrs is None else time_attrs,
format=time_format,
),
) )
super().__init__(widgets, attrs) super().__init__(widgets)
def decompress(self, value): def decompress(self, value):
if value: if value:
...@@ -875,8 +881,8 @@ class SplitHiddenDateTimeWidget(SplitDateTimeWidget): ...@@ -875,8 +881,8 @@ class SplitHiddenDateTimeWidget(SplitDateTimeWidget):
""" """
template_name = 'django/forms/widgets/splithiddendatetime.html' template_name = 'django/forms/widgets/splithiddendatetime.html'
def __init__(self, attrs=None, date_format=None, time_format=None): def __init__(self, attrs=None, date_format=None, time_format=None, date_attrs=None, time_attrs=None):
super().__init__(attrs, date_format, time_format) super().__init__(attrs, date_format, time_format, date_attrs, time_attrs)
for widget in self.widgets: for widget in self.widgets:
widget.input_type = 'hidden' widget.input_type = 'hidden'
......
...@@ -840,7 +840,7 @@ Composite widgets ...@@ -840,7 +840,7 @@ Composite widgets
for the date, and :class:`TimeInput` for the time. Must be used with for the date, and :class:`TimeInput` for the time. Must be used with
:class:`SplitDateTimeField` rather than :class:`DateTimeField`. :class:`SplitDateTimeField` rather than :class:`DateTimeField`.
``SplitDateTimeWidget`` has two optional attributes: ``SplitDateTimeWidget`` has several optional arguments:
.. attribute:: SplitDateTimeWidget.date_format .. attribute:: SplitDateTimeWidget.date_format
...@@ -850,6 +850,16 @@ Composite widgets ...@@ -850,6 +850,16 @@ Composite widgets
Similar to :attr:`TimeInput.format` Similar to :attr:`TimeInput.format`
.. attribute:: SplitDateTimeWidget.date_attrs
.. attribute:: SplitDateTimeWidget.time_attrs
.. versionadded:: 2.0
Similar to :attr:`Widget.attrs`. A dictionary containing HTML
attributes to be set on the rendered :class:`DateInput` and
:class:`TimeInput` widgets, respectively. If these attributes aren't
set, :attr:`Widget.attrs` is used instead.
``SplitHiddenDateTimeWidget`` ``SplitHiddenDateTimeWidget``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -136,11 +136,14 @@ File Uploads ...@@ -136,11 +136,14 @@ File Uploads
* ... * ...
Forms Forms
~~~~~ ~~~~~
* ... * The new ``date_attrs`` and ``time_attrs`` arguments for
:class:`~django.forms.SplitDateTimeWidget` and
:class:`~django.forms.SplitHiddenDateTimeWidget` allow specifying different
HTML attributes for the ``DateInput`` and ``TimeInput`` (or hidden)
subwidgets.
Generic Views Generic Views
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
......
...@@ -37,6 +37,18 @@ class SplitDateTimeWidgetTest(WidgetTest): ...@@ -37,6 +37,18 @@ class SplitDateTimeWidgetTest(WidgetTest):
'<input type="text" class="pretty" value="07:30:00" name="date_1" />' '<input type="text" class="pretty" value="07:30:00" name="date_1" />'
)) ))
def test_constructor_different_attrs(self):
html = (
'<input type="text" class="foo" value="2006-01-10" name="date_0" />'
'<input type="text" class="bar" value="07:30:00" name="date_1" />'
)
widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, time_attrs={'class': 'bar'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
widget = SplitDateTimeWidget(date_attrs={'class': 'foo'}, attrs={'class': 'bar'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
widget = SplitDateTimeWidget(time_attrs={'class': 'bar'}, attrs={'class': 'foo'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
def test_formatting(self): def test_formatting(self):
""" """
Use 'date_format' and 'time_format' to change the way a value is Use 'date_format' and 'time_format' to change the way a value is
......
...@@ -40,3 +40,15 @@ class SplitHiddenDateTimeWidgetTest(WidgetTest): ...@@ -40,3 +40,15 @@ class SplitHiddenDateTimeWidgetTest(WidgetTest):
<input type="hidden" name="date_1" value="12:51:00" /> <input type="hidden" name="date_1" value="12:51:00" />
""" """
)) ))
def test_constructor_different_attrs(self):
html = (
'<input type="hidden" class="foo" value="2006-01-10" name="date_0" />'
'<input type="hidden" class="bar" value="07:30:00" name="date_1" />'
)
widget = SplitHiddenDateTimeWidget(date_attrs={'class': 'foo'}, time_attrs={'class': 'bar'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
widget = SplitHiddenDateTimeWidget(date_attrs={'class': 'foo'}, attrs={'class': 'bar'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
widget = SplitHiddenDateTimeWidget(time_attrs={'class': 'bar'}, attrs={'class': 'foo'})
self.check_html(widget, 'date', datetime(2006, 1, 10, 7, 30), html=html)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment