Django, data’d/m/Y format configuration

Asked

Viewed 3,770 times

1

I am unable to change the Django configuration so that the date format looks like’d/m/Y'. In the form validation, if you enter 22/12/1980, the form is invalid. Informing 12/22/1980 the field date is valid and I can save the record.

I have tried to change the following settings:

py Settings.

LANGUAGE_CODE = 'pt-br'
USE_L10N = False

In the class, the attribute is set to:

data_nascimento = models.DateField(null=False)

Is there any other setting so I can change the date format from’m/d/Y' to’d/m/Y'?

  • Already tried using configuration DATE_FORMAT?

  • Not yet, where should I inform DATE_FORMAT, in Settings.py? Would DATE_FORMAT = ’d/m/Y'?

  • @Anderson I included in Settings.py, but I still have an error in the validation "data_nascimento Enter a valid date.". What would be the right place to inform DATE_FORMAT? Thank you.

  • 1

    Yes, it’s in the settings.py. Try DATE_FORMAT = "%d/%m/%Y", or put this format in DATE_INPUT_FORMAT = ["%d/%m/%Y"].

  • @Andreson Includes DATE_FORMAT and DATE_INPUT_FORMAT in Settings.py, but I still have the bug.

  • I printed the form, the value is being passed correctly to view: <tr><th><label for="id_data_birth">Date of birth:</label></th><td><ul class="errorlist"><li>Enter a valid date. </li></ul><input type="text" name="data_nascimento" value="12/22/2005" required id="id_data_nascimento" /></td></tr>

  • 1

    Anderson, I got it. You’re correct, you just missed an "S" on DATE_INPUT_FORMATS. Can you reply so I can mark your reply? Thank you very much.

Show 2 more comments

1 answer

2


According to the settings of Django, there’s the property DATE_INPUT_FORMATS:

A list of formats that will be accepted when inputting data on a date field.

That is, the list of formats that will be accepted for entry dates in date type fields. By default, the property has the following value:

[
    '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
]

You can change it as you wish, but if you only want a valid format, just do:

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

The above mentioned changes must be made in the file settings.py of the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.