2

I use django (1.8.16) with a ton of plugins ("apps"). Some are:

  • django-ckeditor==4.5.1
  • django-image-cropping==1.0.3
  • django.contrib.admin
  • django-jquery==3.1.0

I want to use django autocomplete-light (dal for short) (3.2.1, current version) in the admin interface. Now I have the following jquery struggle. dal uses jquery but does not load it itself. It leaves it up to the developer. dal comes with a script "select2.js" registering a function under $.select2.

Now the problem. When a plugin like ckeditor or image-cropping loads jquery the function $.select2 is not reachable any more.

I have created a bug report over at dal but one dev says it is my responsability to load jquery first.

So, how do I load jquery, load it before dal needs it and load it only once?

mogoh
  • 992
  • 2
  • 7
  • 27

1 Answers1

2

Per the django-ckeditor docs:

If you have jQuery loaded from a different source just don't set [CKEDITOR_JQUERY_URL] and django-ckeditor will not try to load its own jQuery.

You should make sure you're setting all apps that could load jQuery to not load it, similar to above. Then, use django-jquery to load jquery, making sure that jQuery is available in the console (i.e., try typing console.log($) to see if it's available). Make sure that you put your jQuery template tag above any other template tags loaded by the other libraries.

This should solve any conflicts you're experiencing.

YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • I guess you are right. I don't know how to do this for the admin-plugin. I will do some research on monday. – mogoh Apr 21 '17 at 11:12
  • @mogoh the `django.contrib.admin` doesn't load jQuery so you're in the clear there :). – YPCrumble Apr 21 '17 at 17:21
  • @YPCruble I think this is true for the latest version, but not for 1.8. Why else would jquery be [here](https://github.com/django/django/blob/stable/1.8.x/django/contrib/admin/static/admin/js/jquery.js)? – mogoh Apr 24 '17 at 12:44
  • @mogoh you're correct, thanks for the update! Django's library won't conflict with yours however, because it [uses the `django.jQuery` namespace](https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#contrib-admin-jquery). You could always do something like `window.$ = window.jQuery = django.jQuery;` if you just wanted to use the version of jQuery loaded by the admin. But you can also load your own version of jQuery without any problems. – YPCrumble Apr 24 '17 at 13:19