django-autocomplete-light: Cannot read property 'define' of undefined at dalLoadLanguage

Trying to use DAL for formsets in Django 3.1.2.

Problem described occurs in 3.8.1, 3.8.0, 3.7.0dev0 installed via pip. Does not occur in 3.6.0dev1 and earlier (tried through 3.5.1) Does not occur in the sample project (which installs via git)

Set up autocomplete views and form widgets per documentation tutorial. Included {{form.media}} on page as instructed. Upon page load, select widgets are not populated (just single -----------).

Cannot diagnose further as error is thrown in console:

en.js:2 Uncaught TypeError: Cannot read property 'define' of undefined
    at dalLoadLanguage (en.js:2)
    at autocomplete_light.js:106
    at autocomplete_light.js:217

Both Chrome and Firefox highlight that n is undefined at the n.define call at the start of en.js:

var dalLoadLanguage=function(e){var n;e&&e.fn&&e.fn.select2&&e.fn.select2.amd&&(n=e.fn.select2.amd),n.define("select2/i18n/en",[],function()   ... 

Not sure where to begin trying to resolve.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 9
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same issue with V3.8.1 as well, “fix” was to downgrade DAL to 3.5.1…

I was able to fix this issue without editing other scripts (inserted by external libraries) by adding this hacky script early :

window.addEventListener('load', function () {
  django.jQuery = jQuery
})

My root issue was that two versions of jQuery are inserted on the same page. However, that was quite tricky to remove one version without breaking external libraries. I guess one version of jQuery has select2 functions and one doesn’t.

I also had this issue when trying to load DAL and Handsontable in the same template. Downgrading to DAL 3.5.1 worked. Another thing that worked was to remove the call to <script src="/static/admin/js/jquery.init.js"></script> and replace it with a call to

    <script>
    window.django = {jQuery: jQuery.noConflict(false)};
    </script> 

This is essentially what the init script does, except we’re not removing the JQuery variables from the global script (which I think is what was breaking).

We’re currently facing this issue when using django-admin-filters, which seems to import a separate outdated version of jQuery, and therefore causes the “double jQuery” issue that others are seeing. The symptom, as others have stated is the "Cannot read property ‘define’ of undefined at dalLoadLanguage’

I had the same issue with V3.8.1 as well, “fix” was to downgrade DAL to 3.5.1…

Unfortunately, this “fix” is no longer relevant, since v3.8.0 replaces DOMNodeInserted with MutationObserver. DOMNodeInserted is part of the Mutation Events considered obsolete by W3C and were deprecated in Chrome in July 2024, and likely other Chromium browsers.