Home > database >  Django DateInput widget format
Django DateInput widget format

Time:03-03

I am trying to change the date format from mm/dd/yyyy to dd/mm/yyyy in a DateInput widget. I am trying to change the format the following way.

class Meta:
    model = Patient
    fields = '__all__'
    widgets = {
        'date_of_birth': widgets.DateInput(
            format='%d-%m-%Y',
            attrs={'type': 'date', 'class': 'form-control'})
    }

I noticed that if I don't include the 'type': 'date' attribute the date is interpreted in the correct format, but in that case the widget isn't displayed but just a text-field. So you have to write the date manually (including /). With the DateInput widget that looks like this with dd and mm switched DateInput widget


I have also tried changing the settings.py the following way which also didn't help.

LANGUAGE_CODE = 'en-GB'
TIME_ZONE = 'CET'
USE_L10N = True
USE_TZ = True
DATE_INPUT_FORMATS = ('%d/%m/%Y')

CodePudding user response:

In settings.py set DATE_INPUT_FORMATS as below:

    DATE_INPUT_FORMATS = ['%d/%m/%Y']

And in your ModelForm you could do something like below:

    class ClientDetailsForm(ModelForm):
        date_of_birth = 
        DateField(input_formats=settings.DATE_INPUT_FORMATS)
        
        class Meta:
            model = ModelA

keep in mind, that formats must be defined in the forms.py and not in the models.py.

CodePudding user response:

The issue was with my computer settings. I had the language set to English (US). Changing the language to English (UK) fixed the issue.

  • Related