在 django 中:使用多个插件加载 jquery 的问题 - 或:如何仅加载一次 jQuery? [英] In django: Problems with loading jquery with multiple plugins - or: How to load jQuery only once?

查看:24
本文介绍了在 django 中:使用多个插件加载 jquery 的问题 - 或:如何仅加载一次 jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有大量插件(应用程序")的 django (1.8.16).一些是:

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

我想在管理界面中使用 django autocomplete-light(简称 dal)(3.2.1,当前版本).现在我有以下 jquery 斗争.dal 使用 jquery 但不加载它本身.它留给开​​发人员.dal 带有一个脚本select2.js",用于在 $.select2 下注册一个函数.

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.

问题来了.当 ckeditor 或图像裁剪等插件加载 jquery 时,函数 $.select2 不再可用.

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

我已经在 dal 创建了一个错误报告但是一个dev 说我有责任先加载 jquery.

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

那么,我如何加载 jquery,在 dal 需要它之前加载它并且只加载一次?

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

推荐答案

根据 django-ckeditor 文档:

如果您从其他来源加载了 jQuery,请不要设置 [CKEDITOR_JQUERY_URL],django-ckeditor 将不会尝试加载自己的 jQuery.

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.

您应该确保将所有可以加载 jQuery 的应用程序设置为不加载它,与上面类似.然后,使用 django-jquery 加载 jquery,确保 jQuery 在控制台中可用(即,尝试键入 console.log($) 以查看它是否可用).确保将 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.

这篇关于在 django 中:使用多个插件加载 jquery 的问题 - 或:如何仅加载一次 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆