django-bootstrap-datepicker-plus icon indicating copy to clipboard operation
django-bootstrap-datepicker-plus copied to clipboard

Datepicker is not compatible with Django 2.2 & 3.0 and also with formsets with DAL

Open george72 opened this issue 5 years ago • 2 comments

Datepicker works fine with Django 2.1, django crispy forms and django-autocomplete-light except with formsets. Updating Django to 2.2 or 3.0 shows the calendar on datetime field but it does not respond to a click.

george72 avatar Dec 07 '19 17:12 george72

Using Django 2.1 with latest versions of crispy forms and dal 3.5.0 works in forms without formsets. So far datepicker-plus is only compatible up to Django 2.1

george72 avatar Dec 07 '19 23:12 george72

Hello,

I had the same problem with the datepicker-plus and autocomplete-light...

Now I can say that the root cause is autocomplete-light. With the {{ form.media }} it loads the jQuery script a second time into the html code. Normally this script is loaded in the main HTML template. The result is the following error in the browser console:

Uncaught TypeError: Cannot read property ‘Constructor’ of undefined

Luckily there there was a recent commit in the auotcomplete-light repo that removes the additional loading of the jQuery script: yourlabs/django-autocomplete-light@e46300d

If you don't want to wait until the next release you could write a simple subclass of the autocomplete widget and use that. E.g. like this:

from dal_select2.widgets import ModelSelect2Multiple, Select2WidgetMixin, ModelSelect2
import re
from django.forms.widgets import Media


class CustomSelect2WidgetMixin(Select2WidgetMixin):
    @property
    def media(self):
        # get the default js and css media
        media = super().media

        # remove the jQuery script manually
        # this causes errors with datepicker-plus,
        # see https://github.com/monim67/django-bootstrap-datepicker-plus/issues/42
        # TODO this code patch can be removed in future,
        # because of https://github.com/yourlabs/django-autocomplete-light/commit/e46300d
        regex = re.compile(r"^admin/.*jquery(\.min)?\.js$")
        filtered_js = [item for item in media._js if not regex.search(item)]

        return Media(js=tuple(filtered_js), css=media._css)


class CustomAutoCompleteWidgetSingle(ModelSelect2, CustomSelect2WidgetMixin):
    pass


class CustomAutoCompleteWidgetMultiple(ModelSelect2Multiple, CustomSelect2WidgetMixin):
    pass

mammo0 avatar Dec 19 '19 19:12 mammo0

From v5 the widgets work with formsets #26, compatible with Django 4, and provide better error messages for troubleshooting.

monim67 avatar Nov 27 '22 12:11 monim67