[Django] #30578: L10N settings conflict with SelectDateWidget

38 views
Skip to first unread message

Django

unread,
Jun 18, 2019, 9:17:56 PM6/18/19
to django-...@googlegroups.com
#30578: L10N settings conflict with SelectDateWidget
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Keywords: Locale,
Triage Stage: | SelectDateWidget
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
{{{
#!div style="font-size: 80%"
{{{#!python
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

{{{
#!div style="font-size: 80%"
{{{#!python
DATE_FORMAT="d M Y"
}}}
}}}

Moreover, I also set

{{{
#!div style="font-size: 80%"
{{{#!python
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():

{{{
#!div style="font-size: 80%"
{{{#!python
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?

{{{
#!div style="font-size: 80%"
{{{#!python
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

--
Ticket URL: <https://code.djangoproject.com/ticket/30578>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 19, 2019, 2:37:11 AM6/19/19
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.

-------------------------------------+-------------------------------------
Reporter: fensta | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* version: 2.2 => master
* stage: Unreviewed => Accepted


Comment:

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.
{{{#!python


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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:1>

Django

unread,
Jun 19, 2019, 1:40:28 PM6/19/19
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: Shubham
| Bhagat
Type: Bug | Status: assigned

Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shubham Bhagat):

* status: new => assigned
* owner: nobody => Shubham Bhagat


--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:2>

Django

unread,
Jun 21, 2019, 2:12:13 AM6/21/19
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: Shubham
| Bhagat
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1
* needs_tests: 0 => 1


Comment:

[https://github.com/django/django/pull/11496 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:3>

Django

unread,
Jun 26, 2019, 3:23:32 AM6/26/19
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: Shubham
| Bhagat
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shubham Bhagat):

* needs_tests: 1 => 0


Comment:

[https://github.com/django/django/pull/11496 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:4>

Django

unread,
Jun 26, 2019, 5:09:22 AM6/26/19
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: Shubham
| Bhagat
Type: Bug | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"26d16c07fdc4a297daca554afa6375c70d6d82a9" 26d16c07]:
{{{
#!CommitTicketReference repository=""
revision="26d16c07fdc4a297daca554afa6375c70d6d82a9"
Fixed #30578 - Made SelectDateWidget respect a custom date format when
USE_L10N is disabled.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:5>

Django

unread,
Jul 10, 2020, 9:22:24 AM7/10/20
to django-...@googlegroups.com
#30578: SelectDateWidget doesn't use DATE formats properly when
settings.USE_L10N=False.
-------------------------------------+-------------------------------------
Reporter: fensta | Owner: Shubham
| Bhagat
Type: Bug | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed
Keywords: Locale, | Triage Stage: Accepted
SelectDateWidget |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"00727d384be3c98acb46d3913bd9cc460af08582" 00727d3]:
{{{
#!CommitTicketReference repository=""
revision="00727d384be3c98acb46d3913bd9cc460af08582"
Refs #30578 -- Made SelectDateWidget.format_value() independent of
USE_L10N.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30578#comment:6>

Reply all
Reply to author
Forward
0 new messages