-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from VSEScala/mhvis-patch-20231228-datetimelocal
Fix datetime-local input field
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Custom form fields.""" | ||
|
||
from django import forms | ||
|
||
|
||
class DateTimeControlInput(forms.DateTimeInput): | ||
"""See DateTimeControlField.""" | ||
|
||
input_type = "datetime-local" | ||
|
||
def format_value(self, value): | ||
# The value given seems to be naive. If it wasn't, we would need to | ||
# make it naive first. | ||
return value.strftime("%Y-%m-%dT%H:%M") | ||
|
||
|
||
class DateTimeControlField(forms.DateTimeField): | ||
"""Field for input type datetime-local. | ||
The field requires a very specific format for the value attribute. This | ||
subclass sets the formatted value and the input format correctly. | ||
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local | ||
""" | ||
|
||
input_formats = ("%Y-%m-%dT%H:%M",) | ||
widget = DateTimeControlInput |