Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#30578 closed Bug (fixed)

SelectDateWidget doesn't use DATE formats properly when settings.USE_L10N=False.

Reported by: fensta Owned by: Shubham Bhagat
Component: Forms Version: dev
Severity: Normal Keywords: Locale, SelectDateWidget
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I think I found a bug in Django related to L10N. I'm using a SelectDateWidget in my ModelForm and want it displayed in the order <day, month, year> at the frontend. Therefore, I set

USE_L10N=False

in settings.py so that my custom order specified in DATE_FORMAT takes precedence. In my case, I defined it in settings.py as

DATE_FORMAT="d M Y"

Moreover, I also set

DATE_INPUT_FORMATS = [
    "%d.%m.%Y", 
]

However, validating any dates on the server yielded invalid dates and my custom clean method for my field wasn't called. When looking at the source code for SelectDateWidget, I found value_from_datadict():

def value_from_datadict(self, data, files, name):
        y = data.get(self.year_field % name)
        m = data.get(self.month_field % name)
        d = data.get(self.day_field % name)
        if y == m == d == '':
            return None
        if y is not None and m is not None and d is not None:
           if settings.USE_L10N:  ###### <-- This is the line
                input_format = get_format('DATE_INPUT_FORMATS')[0]
                try:
                    date_value = datetime.date(int(y), int(m), int(d))
                except ValueError:
                    pass
                else:
                    date_value = datetime_safe.new_date(date_value)
                    return date_value.strftime(input_format)
            # Return pseudo-ISO dates with zeros for any unselected values,
            # e.g. '2017-0-23'.
            return '%s-%s-%s' % (y or 0, m or 0, d or 0)
        return data.get(name)

Shouldn't the highlighted if condition be instead?

if not settings.USE_L10N:

As far as I understand the code, without the added negation above the default date format ("year-month-day") will be used all the time. But since I explicitly disabled L10N in the settings, I was expecting that DATE_INPUT_FORMATS will be used instead of the default format. Of course, I could be wrong as I just started with Django, so apologies in advance if I misunderstood something and the code is correct as it is right now.

Thanks in advance,
Stefan

Change History (6)

comment:1 by Mariusz Felisiak, 5 years ago

Summary: L10N settings conflict with SelectDateWidgetSelectDateWidget doesn't use DATE formats properly when settings.USE_L10N=False.
Triage Stage: UnreviewedAccepted
Version: 2.2master

Thanks for the report. I confirmed that there is an issue with this configuration, but I don't think that your patch is correct. get_format() should be used with and without USE_L10N, e.g.

if y is not None and m is not None and d is not None:
    input_format = get_format('DATE_INPUT_FORMATS')[0]
    try:
        date_value = datetime.date(int(y), int(m), int(d))
        date_value = datetime_safe.new_date(date_value)
        return date_value.strftime(input_format)
    except ValueError:
        # Return pseudo-ISO dates with zeros for any unselected values,
        # e.g. '2017-0-23'.
        return '%s-%s-%s' % (y or 0, m or 0, d or 0)
return data.get(name)

Reproduced at 036362e0cfe74e4ab8a65b99eb2aa9c35371fc04.

comment:2 by Shubham Bhagat, 5 years ago

Owner: changed from nobody to Shubham Bhagat
Status: newassigned

comment:3 by Mariusz Felisiak, 5 years ago

Has patch: set
Needs tests: set

comment:4 by Shubham Bhagat, 5 years ago

Needs tests: unset
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 26d16c07:

Fixed #30578 - Made SelectDateWidget respect a custom date format when USE_L10N is disabled.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 00727d3:

Refs #30578 -- Made SelectDateWidget.format_value() independent of USE_L10N.

Note: See TracTickets for help on using tickets.
Back to Top