diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index c468e7d799..7ba916b821 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -855,12 +855,18 @@ class SplitDateTimeWidget(MultiWidget):
supports_microseconds = False
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 = (
- DateInput(attrs=attrs, format=date_format),
- TimeInput(attrs=attrs, format=time_format),
+ DateInput(
+ 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):
if value:
@@ -875,8 +881,8 @@ class SplitHiddenDateTimeWidget(SplitDateTimeWidget):
"""
template_name = 'django/forms/widgets/splithiddendatetime.html'
- def __init__(self, attrs=None, date_format=None, time_format=None):
- super().__init__(attrs, date_format, time_format)
+ def __init__(self, attrs=None, date_format=None, time_format=None, date_attrs=None, time_attrs=None):
+ super().__init__(attrs, date_format, time_format, date_attrs, time_attrs)
for widget in self.widgets:
widget.input_type = 'hidden'
diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt
index 9f3ea2840c..e32cefb0da 100644
--- a/docs/ref/forms/widgets.txt
+++ b/docs/ref/forms/widgets.txt
@@ -840,7 +840,7 @@ Composite widgets
for the date, and :class:`TimeInput` for the time. Must be used with
:class:`SplitDateTimeField` rather than :class:`DateTimeField`.
- ``SplitDateTimeWidget`` has two optional attributes:
+ ``SplitDateTimeWidget`` has several optional arguments:
.. attribute:: SplitDateTimeWidget.date_format
@@ -850,6 +850,16 @@ Composite widgets
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``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt
index 8afafb200d..6b775bec80 100644
--- a/docs/releases/2.0.txt
+++ b/docs/releases/2.0.txt
@@ -136,11 +136,14 @@ File Uploads
* ...
-
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
~~~~~~~~~~~~~
diff --git a/tests/forms_tests/widget_tests/test_splitdatetimewidget.py b/tests/forms_tests/widget_tests/test_splitdatetimewidget.py
index 172bcbbe8d..3b06fd5958 100644
--- a/tests/forms_tests/widget_tests/test_splitdatetimewidget.py
+++ b/tests/forms_tests/widget_tests/test_splitdatetimewidget.py
@@ -37,6 +37,18 @@ class SplitDateTimeWidgetTest(WidgetTest):
''
))
+ def test_constructor_different_attrs(self):
+ html = (
+ ''
+ ''
+ )
+ 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):
"""
Use 'date_format' and 'time_format' to change the way a value is
diff --git a/tests/forms_tests/widget_tests/test_splithiddendatetimewidget.py b/tests/forms_tests/widget_tests/test_splithiddendatetimewidget.py
index 07ee15690c..e3adb11520 100644
--- a/tests/forms_tests/widget_tests/test_splithiddendatetimewidget.py
+++ b/tests/forms_tests/widget_tests/test_splithiddendatetimewidget.py
@@ -40,3 +40,15 @@ class SplitHiddenDateTimeWidgetTest(WidgetTest):
"""
))
+
+ def test_constructor_different_attrs(self):
+ html = (
+ ''
+ ''
+ )
+ 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)