This commit is contained in:
Marcelo Galigniana 2025-09-15 20:45:45 +01:00 committed by GitHub
commit 665ea09525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 90 additions and 2 deletions

View File

@ -342,8 +342,12 @@ class HTMLFormRenderer(BaseRenderer):
# Get a clone of the field with text-only value representation. # Get a clone of the field with text-only value representation.
field = field.as_form_field() field = field.as_form_field()
if style.get('input_type') == 'datetime-local' and isinstance(field.value, str): if style.get('input_type') == 'datetime-local':
field.value = field.value.rstrip('Z') # The format of an input type="datetime-local" is "yyyy-MM-ddThh:mm"
# followed by optional ":ss" or ":ss.SSS", so keep only the first three
# digits of milliseconds to avoid browser console error.
datetime_value = field._field.parent.validated_data.get(field.field_name)
field.value = datetime_value.replace(tzinfo=None).isoformat(timespec="milliseconds").rstrip('Z')
if 'template' in style: if 'template' in style:
template_name = style['template'] template_name = style['template']

View File

@ -1,5 +1,6 @@
import re import re
from collections.abc import MutableMapping from collections.abc import MutableMapping
from datetime import datetime
import pytest import pytest
from django.core.cache import cache from django.core.cache import cache
@ -488,6 +489,89 @@ class TestHiddenFieldHTMLFormRenderer(TestCase):
assert rendered == '' assert rendered == ''
class TestDateTimeFieldHTMLFormRender(TestCase):
"""
Default USE_TZ is True.
Default TIME_ZONE is 'America/Chicago'.
"""
def test_datetime_field_rendering_milliseconds(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField()
appointment = datetime(2024, 12, 24, 0, 55, 30, 345678)
serializer = TestSerializer(data={"appointment": appointment})
serializer.is_valid()
renderer = HTMLFormRenderer()
field = serializer['appointment']
rendered = renderer.render_field(field, {})
self.assertInHTML(
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.345">',
rendered
)
def test_datetime_field_rendering_no_milliseconds(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField()
appointment = datetime(2024, 12, 24, 0, 55, 30, 0)
serializer = TestSerializer(data={"appointment": appointment})
serializer.is_valid()
renderer = HTMLFormRenderer()
field = serializer['appointment']
rendered = renderer.render_field(field, {})
self.assertInHTML(
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.000">',
rendered
)
def test_datetime_field_rendering_no_seconds_and_no_milliseconds(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField()
appointment = datetime(2024, 12, 24, 0, 55, 0, 0)
serializer = TestSerializer(data={"appointment": appointment})
serializer.is_valid()
renderer = HTMLFormRenderer()
field = serializer['appointment']
rendered = renderer.render_field(field, {})
self.assertInHTML(
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:00.000">',
rendered
)
def test_datetime_field_rendering_with_format(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField(format='%a %d %b %Y, %I:%M%p')
appointment = datetime(2024, 12, 24, 0, 55, 30, 345678)
serializer = TestSerializer(data={"appointment": appointment})
serializer.is_valid()
renderer = HTMLFormRenderer()
field = serializer['appointment']
rendered = renderer.render_field(field, {})
self.assertInHTML(
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.345">',
rendered
)
@override_settings(TIME_ZONE='UTC', USE_TZ=True)
def test_datetime_field_utc(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField()
appointment = datetime(2024, 12, 24, 0, 55, 30, 345678)
serializer = TestSerializer(data={"appointment": appointment})
serializer.is_valid()
renderer = HTMLFormRenderer()
field = serializer['appointment']
rendered = renderer.render_field(field, {})
self.assertInHTML(
'<input name="appointment" class="form-control" type="datetime-local" value="2024-12-24T00:55:30.345">',
rendered
)
class TestHTMLFormRenderer(TestCase): class TestHTMLFormRenderer(TestCase):
def setUp(self): def setUp(self):
class TestSerializer(serializers.Serializer): class TestSerializer(serializers.Serializer):