Code

Opened 4 years ago

Closed 4 years ago

#14042 closed (duplicate)

DATE_FORMAT and DATETIME_FORMAT are ignored

Reported by: bendavis78 Owned by: nobody
Component: Core (Other) Version: 1.2
Severity: Keywords: DATE_FORMAT DATETIME_FORMAT admin
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This is the same bug that was reported in #2203, however #2203 was closed during the I18N refactoring, even though the bug has not been completely fixed.

Here's the problem as it exists now. The admin change list calls django.utils.formats.localize() on date fields. This function, in turn, calls date_format() in the same module. The problem is that this is only called in the condition that USE_I10N is set to True. Otherwise, it simply returns the raw value:

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

The date_format() function, however, already takes care of handing USE_I10N correctly via get_format():

def date_format(value, format=None):
    """ 
    Formats a datetime.date or datetime.datetime object using a
    localizable format
    """
    return dateformat.format(value, get_format(format or 'DATE_FORMAT'))

def get_format(format_type):
    """ 
    For a specific format type, returns the format for the current
    language (locale), defaults to the format in the settings.
    format_type is the name of the format, e.g. 'DATE_FORMAT'
    """ 
    format_type = smart_str(format_type)
    if settings.USE_L10N:
        for module in get_format_modules():
            try:
                return getattr(module, format_type)
            except AttributeError:
                pass
    return getattr(settings, format_type)

... so the condition in localize() is not really needed. If we remove that condition, the problem is fixed. DATE_FORMAT and DATETIME_FORMAT from settings now works.

Attachments (1)

date_format_settings_fix.diff (1.2 KB) - added by bendavis78 4 years ago.

Download all attachments as: .zip

Change History (3)

Changed 4 years ago by bendavis78

comment:1 Changed 4 years ago by bendavis78

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

Isn't this #13702?

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.