django-cms 和 jQuery
我有一个 django 网站,使用 django-cms 运行得非常愉快,但现在我想使用 jQuery 包含一些我自己的精美 javascript。我对 django 相当陌生,所以我的问题可能源于此。
Django-cms 本身使用 jQuery,因此如果我将 jquery 添加到标头中,事情就会出乎意料地崩溃。如何添加自己的 jQuery 而不影响 django-cms?
目前,我的 javascript 文件存储在我在项目 settings.py 中定义的媒体根中,并且如上所述,我在标头中引用它们。
当我读到这篇文章时,这似乎是一个愚蠢的问题,但我仍然感到困惑。
编辑::一些代码
我定义了一个媒体根:
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
在我的基本模板中,标头包含
<script src="/media/javascript/jquery.js" type="text/javascript"></script>
<script src="/media/javascript/application.js" type="text/javascript"></script>
application.js 中的 Javascript,但当 django-cms 的东西启动时,它就会崩溃。例如,尝试将插件添加到占位符会导致:
Uncaught TypeError: Property 'type' of object function ( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
} is not a function
我认为这是因为两个 jQuery 相互冲突
::另一个编辑:: 我可能应该补充一点,我使用 django 来托管静态文件只是因为这仍在开发中......
I've got a django site running quite happily with django-cms but now I want to include some of my own fancy javascript using jQuery. I'm rather new to django, so my problems might stem from this.
Django-cms uses jQuery itself, and so if I add jquery to the header - things break rathter unsurprisingly. How do I add my own jQuery without affecting django-cms?
At the moment my javascript files are stored in the media root which I've defined in the projects settings.py and, as mentioned, I reference them in the header.
As I read this, it seems a silly question, but I'm still perplexed.
EDIT::Some Code
I have a media root defined:
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
and in my base template the header includes
<script src="/media/javascript/jquery.js" type="text/javascript"></script>
<script src="/media/javascript/application.js" type="text/javascript"></script>
Javascript in application.js works, but it when the django-cms stuff is up it breaks. For example, trying to add a plugin to a placeholder results in:
Uncaught TypeError: Property 'type' of object function ( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context );
} is not a function
I assumed this was because the two jQuerys were conflicting with each other
::ANOTHER EDIT::
I should probably add that I'm using django to host the static files only because this is still in development...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,链接 django-cms 的捆绑 jQuery 确实解决了所有问题...
唉,它使用版本 1.3.2,但我想我会处理这个问题,而不是现在尝试升级 django-cms。
Well, linking the django-cms's bundled jQuery does fix everything...
Alas, it uses version 1.3.2 but I think I'll deal with that rather than try and upgrade django-cms for now.
一个简单的解决方案:将 JQuery 库保留在标头中,并将所有其他 JS 放在页面的底部,就在
之前。在这种情况下,在管理模式下,您的 jQuery 库将被管理员的副本覆盖,然后所有代码将被添加回其中。
A simple solution: keep your JQuery library in the header and place all other your JS at the boottom of the page, right before
</body>
. In this case in admin mode, your jQuery lib will be overrided by admin's copy, and then all of your code will be added back to it.