Django: Use of DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT in settings.py?

后端 未结 4 657
情歌与酒
情歌与酒 2020-12-17 10:27

I would like to globally (through my entire site, admin and front-end) adjust the way dates and time are displayed to my likings, but I cannot figure out what is going on wi

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 10:53

    You can override DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT and other date/time formats when USE_L10N = True by creating custom format files as described in Django documentation.

    In summary:

    1. Set FORMAT_MODULE_PATH = 'yourproject.formats' in settings.py
    2. Create directory structure yourproject/formats/en (replacing en with the corresponding ISO 639-1 locale code if you are using other locale than English) and add __init__.py files to all directories to make it a valid Python module
    3. Add formats.py to the leaf directory, containing the format definitions you want to override, e.g. DATE_FORMAT = 'j. F Y'.

    Example from an actual project here.

提交回复
热议问题