将jQuery加载到Django [英] Load jQuery into Django

查看:97
本文介绍了将jQuery加载到Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的问题。我试图在Django项目中构建一些AJAX功能。我打算用jQuery。现在,我只是通过Linux在本地运行代码。我一直在测试一些代码 here ,所以我相当确定它的工作原理但是我无法确定将jQuery源代码与我的设置结合在一起。



我下载了 jQuery 并把它放在我认为是我的Media文件夹中。我的settings.py文件如下所示:

  MEDIA_ROOT = os.path.join(PROJECT_DIR,'books / media /') <峰; br> 
MEDIA_URL ='http:// localhost:8000 / books / media /'

我的html模板,我参考:

 < script type =text / javascriptsrc =/ media / js /jquery-1.4.2.min.js\"> 

但是我的功能都没有工作。我相信这是一个愚蠢和显而易见的事情(虽然对我来说显然不是很明显)。如何正确地导入jQuery?

解决方案

您可以将链接硬编码为

 < script type =text / javascriptsrc =/ books / media / js / jquery-1.4.2.min.js> 

或者如果您使用 RequestContext 并使用 django.core.context_processors.media 上下文处理器,那么您可以在模板中访问 MEDIA_URL / p>

 < script type =text / javascriptsrc ={{MEDIA_URL}}} js / jquery-1.4.2。 min.js> 


I have a really basic question. I'm trying to build some AJAX functionality into a Django project. I plan to use jQuery. Right now, I'm just running the code locally through Linux. I've been testing some code here so I'm reasonably certain that it works. But I'm having trouble figuring where to put the jQuery source code in combination with my settings.

I downloaded jQuery and put it in what I think is my Media folder. My settings.py file reads as follows:

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'books/media/')<br>
MEDIA_URL = 'http://localhost:8000/books/media/'

In my html template, I'm referencing:

<script type="text/javascript" src="/media/js/jquery-1.4.2.min.js">

But none of my functions are working. I'm sure it's something stupid and obvious (though clearly not obvious to me). How do I correctly source jQuery?

解决方案

You can hardcode the link as

<script type="text/javascript" src="/books/media/js/jquery-1.4.2.min.js">

or if you render your template with a RequestContext and use the django.core.context_processors.media context processor, then you can access MEDIA_URL in your template.

<script type="text/javascript" src="{{ MEDIA_URL }}}js/jquery-1.4.2.min.js">

这篇关于将jQuery加载到Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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