django-autocomplete-light: cannot use select2 on grappelli admin dashboard

Hi,

I used from autocomplete light in grappelli dashboard but i got this error when load page, Uncaught TypeError: $(...).select2 is not a function select2.js:8!!!

how can i fix this?!

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 4
  • Comments: 37 (15 by maintainers)

Most upvoted comments

Hi everyone,

Just for other which have this same JS error (without grapelli for me) : to fix the issue, drop the static dir before collecting it.

I think the same error should happen with grappelli and the collectstatic command itself show us the fix :

Found another file with the destination path ‘admin/js/jquery.init.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a uniq ue path.

However, I didn’t understand that if the file was previously collected, it won’t be override. So even if INSTALLED_APPS is correctly sorted, the command won’t override the previous admin/js/jquery.init.js from django.contrib.admin (or grapelli).

By removing the file copied in static dir (or dropping the static dir itself), the next collectstatic will properly copied the first one admin/js/jquery.init.js founded, which is the one from dal due to ordering of the setting INSTALLED_APPS.

At least for me, it fix this issue.

I’m not sure we could easily fix it, but we could add some note in install doc? For example:

  1. add dal and dal_select2 in the top of the INSTALLED_APPS
  2. drop your collected static dir (or at least admin/js/jquery.init.js)
  3. collect static file python manage.py collectstatic

Just in case, check that your served jquery.init.js is the one from dal.

Hope it could help.

I’m using the default Django admin and I’m getting the same issue. I’m following the documentation to show a ModelSelect2Multiple - I can get the form to show, but I get the error that the OP mentions.

I also get select2.full.js:473 Select2: An instance of jQuery or a jQuery-compatible library was not found. Make sure that you are including jQuery before Select2 on your web page. - I can solve this with the code below in the admin class…

class Media:
        js = (
            'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
        )

… but I don’t think this is the solution. The installed apps layering does not do the trick either and I continue receiving the same error as the OP.

I’ve ran collectstatic with no luck. I’m guessing I need to load the jQuery file before the select2 based on other answers I’ve seen on StackOverflow.

Has anybody seen something like this before? Django 1.9.2/DAL 3.1.8

I was having this exact issue on 3.5.1, upgraded to 3.7.0dev and it appears to be working.

I’m sorry, I had already released some 3.6.0dev versions back in March 2020 and they have nothing to do with this release, so we’re skipping to 3.7.0

This should be fixed, amongst with many other js order/loading issues by #1162 which I rebased and merged in master 8 days ago

Is it better with 3.7.0 ?

Guys, is this still an issue? Seems like each time I end up here…

That a palliative solution, we need a preventive one.

Le 23 févr. 2017 9:40 PM, “Enrique Paz” notifications@github.com a écrit :

Hi, using Django 1.8 Change the code in: FILE:

select2.full.js LINE: 8

Add this code at the beginning of the file

var $ = django.jQuery;

LINE: 470

old code in file

var _$ = jQuery || $;

New code in File

var _$ = jQuery || $ || django.jQuery;

no aplicts

./manage.py collectstatic

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yourlabs/django-autocomplete-light/issues/602#issuecomment-282114346, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFxrNVXltzk1KQ3_8VAaptBg7RL4bj_ks5rfe7GgaJpZM4HiRRf .