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 4 commits into
base: main
Choose a base branch
from

Conversation

mgaligniana
Copy link
Contributor

@mgaligniana mgaligniana commented Apr 4, 2024

Description

Hi!

This PR delete everything after the first 3 digits of the miliseconds part of a DateTimeField input at HTMLFormRenderer to avoid break in browsers:

image

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/datetime-local#try_it

Fix #5363

@mgaligniana
Copy link
Contributor Author

Hi @auvipy! Following your comment on #5363 (comment) I ask if you could do the review! Thank you!

@auvipy auvipy self-requested a review May 27, 2024 16:08
@auvipy auvipy requested a review from a team June 10, 2024 07:08
Copy link

stale bot commented Apr 27, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 27, 2025
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

please address the review comment

@stale stale bot removed the stale label Apr 28, 2025
@mgaligniana
Copy link
Contributor Author

Thanks @auvipy! I'll do it!

@mgaligniana
Copy link
Contributor Author

Just an update here. I'm still active! I'll do as soon as possible

@mgaligniana
Copy link
Contributor Author

I finally did it, sorry for the long delay!

@mgaligniana mgaligniana requested a review from auvipy July 30, 2025 14:58
@auvipy auvipy requested a review from Copilot August 16, 2025 09:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes HTML5 datetime-local input formatting by truncating milliseconds to 3 digits to ensure browser compatibility. The HTML5 datetime-local input type specification only supports up to 3 digits of milliseconds precision, but DRF was potentially outputting more digits which caused browser validation errors.

  • Modifies the datetime-local field value formatting in HTMLFormRenderer to limit milliseconds to 3 digits
  • Adds a test case to verify the correct datetime-local input formatting

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
rest_framework/renderers.py Updates datetime-local field formatting to truncate milliseconds to 3 digits
tests/test_renderers.py Adds test case to verify datetime-local input formatting with milliseconds

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

auvipy and others added 2 commits August 16, 2025 15:37
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
auvipy
auvipy previously approved these changes Aug 18, 2025
Copy link
Collaborator

@peterthomassen peterthomassen left a comment

Choose a reason for hiding this comment

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

Thanks. I have two more remarks:

  • Timezones currently get dropped, but I assume that's fine because this is about datetime-local which "represent[s] a local date and time, with no time-zone offset information".
  • The transformation assumes that the string format is iso-8601. It doesn't work if I specify, for example, appointment = serializers.DateTimeField(format='%a %d %b %Y, %I:%M%p'). Shouldn't the internal value be used as an input?

@mgaligniana
Copy link
Contributor Author

Thank you Peter!

Not sure if I'm following: do you mean use the datetime value instead of the input.value (string)?

So let me know if you want to fix this in this pull request

@peterthomassen
Copy link
Collaborator

do you mean use the datetime value instead of the input.value (string)?

Yes, that's what I meant. Sorry for being unclear!

@browniebroke
Copy link
Member

I tried to use this branch on a Django project with TIME_ZONE="Europe/Paris" and I'm getting a similar error as the original report, bacause the timezone is not Z but +02:00 at the end. While it feels a bit out of scope of what this fix is trying to do, this last suggestion could address it as well?

@mgaligniana
Copy link
Contributor Author

Correct! I'll go for that approach and add one more test for that case Bruno! Thanks!

@auvipy auvipy dismissed their stale review August 20, 2025 14:06

stale

@auvipy auvipy self-requested a review August 20, 2025 14:07
Comment on lines +349 to +350
datetime_value = field._field.parent.validated_data.get(field.field_name)
field.value = datetime_value.replace(tzinfo=None).isoformat(timespec="milliseconds").rstrip('Z')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was harder than I thought!

Let me know if this is the correct way to get the datetime value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure. Is it guaranteed that there is always a parent?

@@ -488,6 +489,68 @@ class TestSerializer(serializers.Serializer):
assert rendered == ''


class TestDateTimeFieldHTMLFormRender(TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to add a couple of test cases with some non-naive datetimes, with a timezone specified. Could try with UTC and another timezone where the offset is non-zero

@mgaligniana
Copy link
Contributor Author

@browniebroke and @peterthomassen thank you so much for your quick reviews and guidance! 🙏

It would be nice to add a couple of test cases with some non-naive datetimes, with a timezone specified. Could try with UTC and another timezone where the offset is non-zero

Sure, I'll add a new test with timezone!

I'm not sure. Is it guaranteed that there is always a parent?

Good question. I didn't fully understand what is parent so I'll read more code to be sure and add new cases if needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTML5 datetime-local valid format HTMLFormRenderer
5 participants