Skip to content

Fixed #5363 -- HTML5 datetime-local valid format HTMLFormRenderer #9365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ def render_field(self, field, parent_style):
field = field.as_form_field()

if style.get('input_type') == 'datetime-local' and isinstance(field.value, str):
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_parts = field.value.split(".")
field.value = f"{datetime_parts[0]}.{datetime_parts[1][:3]}"
Copy link
Preview

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code assumes that field.value always contains a decimal point and that datetime_parts[1] exists. If field.value doesn't contain milliseconds (e.g., '2024-12-24T00:55:30'), this will raise an IndexError when trying to access datetime_parts[1].

Suggested change
field.value = f"{datetime_parts[0]}.{datetime_parts[1][:3]}"
if len(datetime_parts) == 2:
# Truncate milliseconds to 3 digits
field.value = f"{datetime_parts[0]}.{datetime_parts[1][:3]}"
else:
# No milliseconds present; leave value as is or append ".000" if needed
field.value = field.value

Copilot uses AI. Check for mistakes.


if 'template' in style:
template_name = style['template']
Expand Down
18 changes: 18 additions & 0 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from collections.abc import MutableMapping
from datetime import datetime

import pytest
from django.core.cache import cache
Expand Down Expand Up @@ -488,6 +489,23 @@ class TestSerializer(serializers.Serializer):
assert rendered == ''


class TestDateTimeFieldHTMLFormRender(TestCase):
def test_datetime_field_rendering(self):
class TestSerializer(serializers.Serializer):
appointment = serializers.DateTimeField()

appointment = datetime(2024, 12, 24, 00, 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):
def setUp(self):
class TestSerializer(serializers.Serializer):
Expand Down
Loading