Opened 9 months ago
Last modified 9 months ago
#35255 closed Bug
Inconsistent Behavior of SelectDateWidget._parse_date_fmt() — at Initial Version
Reported by: | Peter-Gehlert | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 5.0 |
Severity: | Normal | Keywords: | SelectDateWidget Localization DATE_FORMAT |
Cc: | Peter-Gehlert | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
When looking to change the format of the date in the SelectDateWidget from the default of Month, Day, Year to Day, Month, Year; all advice i found pointed to changing the DATE_FORMAT in settings.py however I discovered an error with the implementation in the widget that prevents this from changing the format as expected.
When generating the contextwidgetsubwidgets in the SelectDateWidget.get_context() method, the implementation relies on the SelectDateWidget._parse_date_fmt() static method to determine the date format.
@staticmethod
def _parse_date_fmt():
fmt = get_format("DATE_FORMAT")
The current implementation of _parse_date_fmt() disregards the lang and use_l10n parameters, leading to inconsistencies in date formatting. Specifically, the use_l10n parameter is always set to True by default within the get_format() function:
if use_l10n is None:
use_l10n = True
if use_l10n and lang is None:
lang = get_language()
This results in the function always returning the default date format as specified by the LANGUAGE_CODE setting or the language determined by USE_I18N regardless of any language or localization preferences specified elsewhere.
Expected Behavior:
The _parse_date_fmt() method should take into account the lang and use_l10n parameters to allow for language-specific and localization-aware date formatting. This would ensure that the generated date format respects the desired language and localization settings.
Recommendation:
Modify the _parse_date_fmt() method to properly handle the lang and use_l10n parameters when retrieving the date format. This can be achieved by passing these parameters to the get_format() function within the method.
@staticmethod
def _parse_date_fmt(lang=None, use_l10n=None):
fmt = get_format(
"DATE_FORMAT",
lang=translation.get_language(),
use_l10n=getattr(django.conf.settings, 'USE_L10N')
)