0
In django, widgets
are responsible for rendering the view. so you might expect that rendering a non-formatted DateTimeInput
cause this unexpected behavior.
Try doing this:
class OfferForm(forms.ModelForm):
[...]
class Meta:
model = Offer
fields = (
[...]
'expiry_date'
widgets = {
[...]
'expiry_date': forms.DateTimeInput(
attrs={'type': 'datetime-local',
'class': 'form-control', },
format='your-desired-format'
)
}
Also add your desired format to your model field supported formats
input_formats = ['list-of-desired-formats']
For more details here: docs
0
I have finally decided to use a different approach and simply pass an integer representing an offset in days.
Thank you to @Ramy Mohamed for his insight.
I was trying to POST the datetime object as-is. Since the widget is rendered as a text input, the server would receive a string and not a datetime object after a POST request.
I didnβt need to configure the format as shown in his answer though, since he was talking about how the datetime would be displayed, not fed back to the server.
This is what I had done that worked:
test_views.py
locale_format = formats.get_format('DATETIME_INPUT_FORMATS', lang=translation.get_language())[0]
offer['expiry_date'] = offer['expiry_date'].strftime(locale_format)
- [Django]-Changing package install order in Python
- [Django]-Import data into Django model with existing data?
- [Django]-How to save ManyToMany field in second database