Django管理员显示本地化的日期格式

时间:2014-01-22 22:50:23

标签: django date django-admin format

我住在新西兰,日期格式为DD / MM / YYYY,但是admin中的日期字段显示YYYY-MM-DD,我尝试通过以下方式更改小部件:

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

来自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.

同样在我的设置中:

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')

但仍然无效。

1 个答案:

答案 0 :(得分:1)

只有在DATE_INPUT_FORMATS

时才会尊重DATETIME_INPUT_FORMATS中的settings.pyUSE_I18N = False

如果你有USE_I18N = True,你还需要使用FORMAT_MODULE_PATH并创建一些格式模块文件,如下所述:
https://docs.djangoproject.com/en/1.4/ref/settings/#format-module-path