开发者

Django admin - jQuery namespace

开发者 https://www.devze.com 2023-03-31 04:31 出处:网络
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)

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 开发者_如何学Cfor 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 ?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号