Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin - jQuery namespace

I'm trying to use certain jQuery plugins in my Django admin site.

Django admin sets the jQuery namespace to django.jQuery (to avoid conflicts)

If I don't have the default $ namespace for jQuery, the plugins won't work, will they ?

Do I have to do something like

window.$ = django.jQuery

?

How & where can I change this namespace for the whole admin site ?

like image 819
Pierre de LESPINAY Avatar asked Aug 25 '11 10:08

Pierre de LESPINAY


3 Answers

Actually, most plugins will require "jQuery" - not $ - to be available, and then provide $ themselves as in dmidz's answer.

Therefore, insert

var jQuery = django.jQuery;

before your external references. If you're loading a bunch of thirdparty jQuery plugins, put the above line in a script tag that preceeds the plugins.

See also my question five months ago How to provide $ to third-party, external jQuery plugins in Django admin

like image 187
Danny W. Adair Avatar answered Nov 10 '22 20:11

Danny W. Adair


I suggest you to leave the jQuery in django.jQuery namespace wich is a good idea when using cms with different modules that could conflict. But you wrap your plugin within such :

;(function($){
  // here $ is only in this scope and so totally inobrusive
  // plugin code
})(django.jQuery);
like image 29
dmidz Avatar answered Nov 10 '22 20:11

dmidz


Someone else had a similar problem and used the deconflict function: http://www.lokkju.com/blog/archives/143

I'm not sure what version Django uses, but I think the point of using the django namespace is that it can use its own version of jQuery for its internal operations, but still let you use a different version for your own work.

like image 24
Dennis Avatar answered Nov 10 '22 21:11

Dennis