European date input in Django Admin

前端 未结 6 1245
-上瘾入骨i
-上瘾入骨i 2021-01-02 13:30

Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which format to use when viewing dates, but doesn\'t apparently let me change the input forma

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 13:43

    There is an official way to do this now since the closing of Django ticket 6483 & release of Django 1.2.

    If you have USE_L10N set to False, what you should do is specify the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS in your settings.py. Here are the settings I use for this, based on converting the defaults:

    #dd/mm/yyyy and dd/mm/yy date & datetime input field settings
    DATE_INPUT_FORMATS = ('%d-%m-%Y', '%d/%m/%Y', '%d/%m/%y', '%d %b %Y',
                          '%d %b, %Y', '%d %b %Y', '%d %b, %Y', '%d %B, %Y',
                          '%d %B %Y')
    DATETIME_INPUT_FORMATS = ('%d/%m/%Y %H:%M:%S', '%d/%m/%Y %H:%M', '%d/%m/%Y',
                              '%d/%m/%y %H:%M:%S', '%d/%m/%y %H:%M', '%d/%m/%y',
                              '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%Y-%m-%d')
    

    If you have USE_L10N set to True, then you will need to use the FORMAT_MODULE_PATH instead.

    For example, my LANGUAGE_CODE is set to en-au, my site is called golf, and my FORMAT_MODULE_PATH is set to golf.formats, so my directory structure looks like this:

    golf/
        settings.py
        ...
        formats/
            __init__.py
            en/
                __init__.py
                formats.py
    

    and the DATE_INPUT_FORMATS and DATETIME_INPUT_FORMATS settings are in formats.py instead of settings.py.

提交回复
热议问题