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

Asked

Viewed 1,327 times

5

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

1 answer

7


First so that the time/date setting is done manually, enter the file settings.py and change the value of USE_L10N for False, otherwise Django will read from its default files and set the date according to them.

The variable DATE_FORMATis a string which stores the format that the date will be displayed on your system. For Brazil, we normally use the value’d/m/Y' (08/02/2015), thus, no settings.py this variable shall be declared as follows::

DATE_FORMAT = 'd/m/Y'

The other variable you should change so that Django formats your date correctly in inputs (forms) is DATE_INPUT_FORMATS who’s kind tuple. The values of this tuple are read in the assignment order, which means that if the first format of the input does not match the first value, it tries to match the second and so on. We will use, in this case, the same format of the variable above, but with a difference:

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

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

Now you can use these settings to make date display and formatting common to your entire system.

Browser other questions tagged

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