Django admin to display localised date format

依然范特西╮ 提交于 2019-12-12 01:09:13

问题


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



来源:https://stackoverflow.com/questions/21295721/django-admin-to-display-localised-date-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!