How to set date formats in Django 1.7 for the whole system

4

Is there any way to change display settings and / or date formatting at system level?

    
asked by anonymous 08.02.2015 / 17:30

1 answer

6

First, to set the time / date manually, enter the file settings.py and change the value from USE_L10N to False , otherwise Django will read from your default files and set the date accordingly with them.

The DATE_FORMAT variable is a string that stores the format the date will be displayed on your system. For Brazil, we usually use the value 'd / m / Y' (08/02/2015), so in settings.py this variable will be declared as follows:

DATE_FORMAT = 'd/m/Y'

The other variable that you must change in order for Django to format your date correctly in inputs (forms) is DATE_INPUT_FORMATS which is of type tuple . The tuple values are read in the assignment order, which means that if the first input format does not match the first value, it tries to match the second onwards. In this case, we will use the same format as the above variable, but with a difference:

DATE_INPUT_FORMATS = (
    '%d/%m/%Y',
)

This time, the value string that we put in this tuple follows the format that Python uses to format dates, so Django will format them automatically without our intervention.

You can now use these settings to make viewing and formatting dates common to your entire system.

    
08.02.2015 / 17:30