Opened 9 months ago

Closed 9 months ago

#35255 closed Bug (invalid)

Inconsistent Behavior of SelectDateWidget._parse_date_fmt()

Reported by: Peter-Gehlert Owned by: Sagar Sadhu
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 (last modified by David Sanders)

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')
    )

Change History (5)

comment:1 by David Sanders, 9 months ago

Description: modified (diff)

comment:2 by David Sanders, 9 months ago

Have you tried setting the DATE_FORMAT in your custom language format file as per: https://docs.djangoproject.com/en/5.0/topics/i18n/formatting/#creating-custom-format-files ?

comment:3 by Sagar Sadhu, 9 months ago

Owner: changed from nobody to Sagar Sadhu
Status: newassigned

comment:4 by Sagar Sadhu, 9 months ago

Triage Stage: UnreviewedAccepted

comment:5 by David Sanders, 9 months ago

Resolution: invalid
Status: assignedclosed
Triage Stage: AcceptedUnreviewed

Resolving as invalid assuming there was a misunderstanding on how the new localisation settings work in Django 5.0. If this is a feature request please head over to the Django forum to discuss proposal to make changes 👍 https://www.djangoproject.com/community/

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