Why does django not use my SHORT_DATE_FORMAT with date template tag?

让人想犯罪 __ 提交于 2019-12-07 18:35:23

问题


I have troubles understanding why django doesn't use my SHORT_DATE_FORMAT in templates when I specify it for the date template tag. My settings are:

TIME_ZONE = 'Australia/Melbourne'
SHORT_DATE_FORMAT = 'd/m/Y'
LANGUAGE_CODE = 'en-AU'
USE_I18N = True
USE_L10N = True
USE_TZ = True

In my template:

{{ asset.upload_date|date:"SHORT_DATE_FORMAT" }}

I would expect '21/01/2014' but I get '01/21/2014'.


回答1:


It's actually due to USE_L10N=True + Django still not having a locale conf for Australia (en-au/en_AU) which specifies d/m/Y. Bit of an surprise. But it will be there with the release of 1.7

https://code.djangoproject.com/ticket/21237

https://groups.google.com/forum/#!topic/django-users/Bgx3u1xtaMc




回答2:


The easiest way to fix this for 1.6 is create a locale folder in your app folder and to set

FORMAT_MODULE_PATH = 'myapp.locale'

then copy the en_AU folder from

https://github.com/django/django/tree/master/django/conf/locale

and then add a init.py file to the locale folder with

LANG_INFO = {
    'en-au': {
        'bidi': False,
        'code': 'en-au',
        'name': 'Australian English',
        'name_local': 'Australian English',
    },
}


来源:https://stackoverflow.com/questions/21420148/why-does-django-not-use-my-short-date-format-with-date-template-tag

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