mirror of
https://github.com/django/django.git
synced 2025-01-08 17:37:20 +00:00
Fixed #5851 -- Allowed specifying different HTML attrs for SplitDateTimeWidget subwidgets.
Thanks Tim Graham and Nick Pope for review.
This commit is contained in:
parent
3a148f958d
commit
0034e9af18
@ -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'
|
||||
|
||||
|
@ -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``
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
@ -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
|
||||
~~~~~~~~~~~~~
|
||||
|
@ -37,6 +37,18 @@ class SplitDateTimeWidgetTest(WidgetTest):
|
||||
'<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):
|
||||
"""
|
||||
Use 'date_format' and 'time_format' to change the way a value is
|
||||
|
@ -40,3 +40,15 @@ class SplitHiddenDateTimeWidgetTest(WidgetTest):
|
||||
<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)
|
||||
|
Loading…
Reference in New Issue
Block a user