[Django]-Django: Form widget not accepting date format provided by bootstrapdatetimepicker

2πŸ‘

βœ…

It appears that the JS library and DateTimeInputField are correctly configured to allow the new datetime format, but the model cannot validate the new datetime format. Accordingly it will not save the data.

If this is correct, settings.py can be modified to allow for different input formats.
Be sure that USE_L10N = False otherwise it will override the custom formats listed below.

Add the following to the bottom of settings.py:

from django.conf.global_settings import DATETIME_INPUT_FORMATS, DATE_INPUT_FORMATS
DATE_INPUT_FORMATS += ("%d-%m-%Y",)
DATETIME_INPUT_FORMATS += ("%d-%m-%Y %H:%M:%S",)
πŸ‘€Dylan

1πŸ‘

I had the same problem and i solved it by defining the field in ModelForm:

date = forms.DateTimeField(input_formats=['%d-%m-%Y %H:%M:%S']

class Meta:
    model = Quotation
    fields = [
        'full_name',
        'organisation',
        'address',            
        'date',
        'type',            
        'description',
        'budget',
    ]
    def __init__(self, *args, **kwargs):
        super(YourModelName, self).__init__(*args, **kwargs)
        self.fields['date'].widget.attrs.update({'class': 'form-control', 'id': 'm_datetimepicker_1'})

Also modify your javascript format to β€˜dd-mm-yyyy hh:ii:ss’

πŸ‘€alex

Leave a comment