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

后端 未结 4 633
情歌与酒
情歌与酒 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:42

    Searching through the source shows that DATETIME_FORMAT, etc., are only used when django.utils.formats.localize() is called, and that only seems to be called when django.template.VariableNodes are rendered.

    I'm not sure when exactly VariableNodes are used in template rendering, but I would guess that if you have settings.USE_L10N turned on and you have a VariableNode, it will be localized.

    localize looks like this:

    def localize(value):
        """
        Checks if value is a localizable type (date, number...) and returns it
        formatted as a string using current locale format
        """
        if settings.USE_L10N:
            if isinstance(value, (decimal.Decimal, float, int)):
                return number_format(value)
            elif isinstance(value, datetime.datetime):
                return date_format(value, 'DATETIME_FORMAT')
            elif isinstance(value, datetime.date):
                return date_format(value)
            elif isinstance(value, datetime.time):
                return time_format(value, 'TIME_FORMAT')
        return value
    

    To answer your question, I'd probably write a quick context processor that called localize() on everything in the context.

提交回复
热议问题