-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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
base: main
Are you sure you want to change the base?
Conversation
Hi @auvipy! Following your comment on #5363 (comment) I ask if you could do the review! Thank you! |
rest_framework/renderers.py
Outdated
# The format of an input type="datetime-local" is "yyyy-MM-ddThh:mm" | ||
# followed by optional ":ss" or ":ss.SSS", so remove [milli|micro]seconds | ||
# to avoid browser error. | ||
field.value = "".join(field.value.rstrip('Z').split(".")[:1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The millisecond part should be retained, as it's a valid part of the format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally did it! Thanks for the pacience!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Is field.value
(on the right-hand side) guaranteed to contain a millisecond part (source?)? If not, this code will produce values ending in .
(not followed by digits), which is invalid.
Please add two test cases, one with 0 milliseconds and one with 0 seconds 0 milliseconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I'll do it!
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. |
There was a problem hiding this 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
Thanks @auvipy! I'll do it! |
Just an update here. I'm still active! I'll do as soon as possible |
I finally did it, sorry for the long delay! |
There was a problem hiding this 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.
# 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]}" |
There was a problem hiding this comment.
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].
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.
Co-authored-by: Copilot <[email protected]>
Description
Hi!
This PR delete everything after the first 3 digits of the miliseconds part of a
DateTimeField
input atHTMLFormRenderer
to avoid break in browsers:https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/datetime-local#try_it
refs #5363