monim67 / django-bootstrap-datepicker-plus

Bootstrap3/Bootstrap4/Bootstrap5 DatePickerInput, TimePickerInput, DateTimePickerInput, MonthPickerInput, YearPickerInput with date-range-picker functionality for django >= 2.0
https://pypi.python.org/pypi/django-bootstrap-datepicker-plus
MIT License
223 stars 61 forks source link

[BUG] Unable to make the Date(Time)PickerInput() widget use the current locale #112

Open swiss-knight opened 8 months ago

swiss-knight commented 8 months ago

Hello,

I'm using a DatePickerInput() widget in a form in order to let a user select a date using this nice calendar widget within a django 4.2 application.

The locale is set as follow in the settings.py file:


LANGUAGE_CODE = "fr-FR"
TIME_ZONE = "Europe/Paris"
USE_I18N = True
USE_L10N = True
USE_TZ = True

The form is as follow:

from django import forms
from .app.model import MyModel
from bootstrap_datepicker_plus.widgets import DatePickerInput

class MyForm(forms.ModelForm):
   # stuff
    class Meta:
        model = MyModel
        fields = [
            "user_id",
            "created_at",
            "some_other_field",
        ]

        widgets = {
            "user_id": forms.NumberInput(),
            "created_at": DatePickerInput(),
            "some_other_field": forms.NumberInput(),
        }

And the corresponding app model is:

from django.db import models
from django.utils import timezone

class MyModel(models.Model):
    user_id = models.ForeignKey(
        User,
        null=True,
        on_delete=models.SET_NULL,
        verbose_name=_("User identifier"),
    )
    created_at = models.DateField(
        default=timezone.now,
        verbose_name=_("Date of creation"),
    )
    some_other_field = models.IntegerField(
        verbose_name=_("Some other field"),
    )

The widget looks as follow when it's set to the 9th of January 2024: image

But it should be 09.01.2024 because the browser language is set to French. And when I watch at what is stored in the db, it's written 2024-09-01 (1st September 2024).

So I have to hard code that in the widget options in MyForm() class:

    DatePickerInput(
        options={
            "format": "DD.MM.YYYY",
            "locale": "fr-FR",
            "useCurrent": False,
        }
    ),

(There is no fancy templating:

{% load i18n %}
{% load bootstrap4 %}

{% block content %}
  <form method="POST">
    {% csrf_token %}
    {{ form.media }}
    {% bootstrap_form form layout='horizontal' %}

    <div>submission buttons</div>
  </form>
{% endblock %}

and nothing special in the views )

Then it works within a French browser environment.

But now, when browsing the app using an English browser, the widget stays as it is coded (this is normal I guess): image

and the user can no more submit the form because of a server error:

form.errors: <ul class="errorlist"><li>created_at<ul class="errorlist"><li>Enter a valid date.</li></ul></li></ul>

Expected behavior So I would like to know if it's possible to make the DatePickerInput() widget be aware of the locale? (Same goes for the DateTimePickerInput() widget).

If yes, how precisely?

For the moment, I'm thinking it's a bug or a missing feature.

Thanks a lot!

Setup Information (please complete the following information):

[x] I have followed the configuration instructions and checked out the common error troubleshooting page.

mkulmer commented 7 months ago

Hello,

I have the same problem. All but hard coding the locale and format in each DatePickerInput's widget options fails. It seems, at least in my case, that datepicker-plus somehow fetches the global options from the server it is running on:

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME=C

instead of the settings in settings.py:

LANGUAGE_CODE = 'de-DE'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
...
BOOTSTRAP_DATEPICKER_PLUS = {
    "options": {
        "locale": "de-DE",
    },
    "variant_options": {
        "date": {
            "format": "DD.MM.YYYY",
        },
    },
}

For reference, my setup information: