Django Accepting Am/pm As Form Input
I am trying to figure out how to accept am/pm as a time format in Django using a DateTime field, but I am having some trouble. I have tried setting it like this in my forms.py file
Solution 1:
DATETIME_INPUT_FORMATS doesn't work for formats containg %p.
Neither do input_formats
kwarg.
As a quick work around, you can alter the input_formats
attribute of DateTimeField
in the Form
's __init__
method.
For example:
classDateTimeForm(forms.Form):
CUSTOM_FORMAT = '%m/%d/%y %I:%M %p'
date_input = forms.DateTimeField(widget=forms.HiddenInput)
def__init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
ifself.CUSTOM_FORMAT notinself.fields['date_input'].input_formats:self.fields['date_input'].input_formats.append(self.CUSTOM_FORMAT)
Django documentation does say that %p cannot be used in parsing date fields.
But if you go through the source code, the to_python method of DateTimeField doesn't really enforce anything like that.
Post a Comment for "Django Accepting Am/pm As Form Input"