Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46208

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 ?

Upvotes: 8

Views: 7512

Answers (3)

Danny W. Adair
Danny W. Adair

Reputation: 12968

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

Upvotes: 13

Dennis
Dennis

Reputation: 32608

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.

Upvotes: 2

dmidz
dmidz

Reputation: 896

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

Upvotes: 4

Related Questions