django-autocomplete-light icon indicating copy to clipboard operation
django-autocomplete-light copied to clipboard

Cannot read property 'define' of undefined at dalLoadLanguage

Open quinceleaf opened this issue 4 years ago • 21 comments

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.

quinceleaf avatar Oct 27 '20 02:10 quinceleaf

Does it work when you load jQuery prior to form.media ?

https://github.com/yourlabs/django-autocomplete-light/issues/1196#issuecomment-721308318

jpic avatar Nov 10 '20 10:11 jpic

I am also running into this problem after ensuring jQuery is loaded.

vecchp avatar Nov 20 '20 21:11 vecchp

I have the same issue with V3.8. It will also have the following error in django admin: The language file for "./i18n/en-us" could not be automatically loaded. A fallback will be used instead. Same issue with https://github.com/yourlabs/django-autocomplete-light/issues/782 But V3.5.1 works well.

aliceni avatar Dec 17 '20 06:12 aliceni

Can you reproduce in an example project so that we can have our hands on your use case and try to debug it for you ?

jpic avatar Dec 17 '20 06:12 jpic

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

wandrogo avatar Jan 30 '21 02:01 wandrogo

I am experiencing the same problem with Django 3.1.6 + DAL 3.8.1. I don't have an example project at the moment in which I can reproduce it, although I may try to make a simplified one. I think that the issue appears to be present in all of the views in which I am using DAL.

I'm wondering if the issue relates to either jQuery or select2 being loaded more than once? Right now I'm loading jquery.min.js, jquery.init.js, and assigning "$" and "django" to django.jQuery all in the

of my document. However, when I look at the source code where form.media appears, I'm seeing related scripts loaded in the following order:
  • "/static/admin/js/vendor/select2/select2.full.js"
  • "/static/admin/js/vendor/jquery/jquery.js"
  • "/static/autocomplete_light/autocomplete_light.js"
  • "/static/admin/js/jquery.init.js"
  • "/static/autocomplete_light/select2.js"
  • "/static/autocomplete_light/i18n/en.js"

None of these are called directly by my form's form.media, though I am using a couple of other extensions (django-filer, Django CMS) that could have something to do with it.

I'm continuing to investigate, if I can get a demo project working that replicates the issue I'll post it here.

leetucker avatar Feb 20 '21 00:02 leetucker

For sure, the best is to load only one jquery @danielmorell any recommendation perhaps?

jpic avatar Feb 20 '21 07:02 jpic

FYI, in case this helps anyone, I was eventually able to resolve my issue. In my case, I was getting this error because django-filer was loading a second copy of jQuery through its widget media. I was already manually including jQuery via the template. So, I fixed it by modifying the form media() method to drop jQuery from the list of media assets.

I definitely look forward to the release of django-autocomplete-light with Webcomponents. Diagnosing issues like this can be pretty tricky!

leetucker avatar Mar 31 '21 02:03 leetucker

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

it woeked for me!!!

JupyterChu avatar Mar 31 '21 09:03 JupyterChu

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

This worked for me as well! Python 3.9, Django 3.1.7.

anshul-aggarwal avatar Apr 04 '21 16:04 anshul-aggarwal

I definitely look forward to the release of django-autocomplete-light with Webcomponents. Diagnosing issues like this can be pretty tricky!

Is this the only possible definitive patch to the problem - is there any other intermediate fix that can be pushed?

gamesbook avatar Apr 05 '21 13:04 gamesbook

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

im obsessive with updates :( i did this

sae13 avatar Jul 30 '21 12:07 sae13

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

nlarusstone avatar Aug 25 '21 15:08 nlarusstone

I also have the same issue. Actually I do not have django.contrib.admin in my INSTALLED_APPS and adding it would solve the issue. Any clue on how to fix this issue without admin app enabled?

j-bernard avatar Aug 31 '21 15:08 j-bernard

Ultimately the solution is removing JS layers, as in #1170 with the autocomplete-light component, it's not in my short term plans to finish it, but it's basically working and ready to finish with new examples, documentation and test cases.

It also requires web components support from the browser and last time I checked safari was lagging at that too, so there's also that.

jpic avatar Sep 01 '21 09:09 jpic

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.

gnuletik avatar Jan 18 '22 19:01 gnuletik

Hi, I have solved this problem by removing "defer" from other scripts

adiletto64 avatar Jun 01 '22 02:06 adiletto64

3.5.1

This worked

sajeell avatar Oct 24 '22 00:10 sajeell