Django admin to display localised date format

2019-08-23 00:05发布

问题:

I live in New Zealand and the date format is DD/MM/YYYY, however the date field in admin shows YYYY-MM-DD, I have tried changing the widget by:

    formfield_overrides = { 
        models.DateField: {'widget': widgets.DateInput},
    } 

Since from the documentation:

DateInput
class DateInput
Date input as a simple text box: <input type='text' ...>

Takes same arguments as TextInput, with one more optional argument:

format
The format in which this field’s initial value will be displayed.

If no format argument is provided, the default format is the first format found in DATE_INPUT_FORMATS and respects Format localization.

Also in my settings:

USE_I18N = True
TIME_ZONE = 'Pacific/Auckland'
LANGUAGE_CODE = 'en-NZ' # or en-nz? both don't work
DATE_INPUT_FORMATS = ('%d/%m/%Y', '%Y-%m-%d')
DATETIME_INPUT_FORMATS = ('%d/%m/%Y %H:%M:%S', '%Y-%m-%d %H:%M:%S')

But still doesn't work.

回答1:

DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS in settings.py are only respected when USE_I18N = False

If you have USE_I18N = True you need to use FORMAT_MODULE_PATH also and create some format module files, as described here:
https://docs.djangoproject.com/en/1.4/ref/settings/#format-module-path