Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin and autocomplete light: Select2: An instance of jQuery or a jQuery-compatible library was not found

Autocomplete light widget doesn't load completely for the field i'm trying to code it on in admin. Instead, I get the following

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.

Alongside a select box with only one option.

This is similar to actualy several questions that have been asked previously about the select2 library, and most of the solutions have involved reordering files in settings.py or running python manage.py collectstatic. But none of those have worked so far for me.

The task is to include jQuery before Select2 on my web page... how can that be done?

like image 270
Byron Smith Avatar asked Nov 08 '22 18:11

Byron Smith


1 Answers

I faced this same issue and I was able to solve it by reconfiguring INSTALLED_APPS in my settings.py file to ensure that 'dal' and 'dal_select2' are both included before 'django.contrib.admin'.

This little tid-bit is included in the installation documentation for django-autocomplete-light, though I missed it in my initial read-through:

Then, to let Django find the static files we need by adding to INSTALLED_APPS, before django.contrib.admin and grappelli if present:

'dal',
'dal_select2',
# 'grappelli',
'django.contrib.admin',

This is to override the jquery.init.js script provided by the admin, which sets > up jQuery with noConflict, making jQuery available in django.jQuery only and not $.

P.S. I ended up finding out that django 2.x+ now has a built-in for auto-complete fields. I ended up using this instead and highly recommend it for lower overhead/development cost.

like image 166
Daniel Long Avatar answered Nov 14 '22 23:11

Daniel Long