如何使用jQuery设置Django网站 [英] How to set up Django website with jQuery

查看:131
本文介绍了如何使用jQuery设置Django网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,但我无法弄清楚如何在我的网站上激活jQuery。所有做教程的网站在Django上启动jQuery开始于JQuery已经在他们的网站上工作。无论如何,我决定下载它并将其放在我的文件夹的某个地方,我决定去欺骗方式并使用Google。
因此,在我的基页中,我有以下代码片段

I don't know why, but I couldn't figure out how to activate jQuery on my website. All sites doing tutorials 'Starting jQuery on Django' started with JQuery already working on their site. Anyway, instead of downloading it and putting it in my folder somewhere, I decided to go the cheat-way and use Google's. Therefore, in my base page, I had the following piece of code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
</script>

一切都很好,直到我不得不添加一些库。我如何设置自己的jQuery,以便它与我的模板一起工作?

And everything worked great, until I had to add some libraries. How do I set up my own jQuery so that it works with my templates?

推荐答案

最后,原来很简单。这是一个必须做的:

In the end, it turned out to be very easy. This is all one has to do:

首先,在你的文件夹应用程序中创建一个名为 static 的文件夹: p>

First, make a folder named static inside your folder app:

mySite
---mytemplates
---mySite
---myApp
------static

然后从他们的网站下载jQuery here 。您点击下载,它将带您到具有所有代码的其他页面。左键单击代码,然后选择另存为...。将其保存在 static 文件夹中。

Then download jQuery from their site here. You click 'Download' and it will take you to a different page with all the code. Left click on the code and select 'save as...'. Save it in the static folder.

在您的 settings.py 文件内确保 django.contrib.staticfiles INSTALLED_APPS (默认情况下是这样)。

Inside your settings.py file make sure that django.contrib.staticfiles is under INSTALLED_APPS (it is there by default).

最后,您的基页内有

<script type="text/javascript" src="{{ STATIC_URL }} /static/jquery-1.8.0.js">
</script> 
//Make sure that the jQuery name is correct. With updates and different versions, 
//the number after 'jquery' will change 

现在你可以在你的网站上使用jQuery! (只要页面扩展你的基础页面,如果没有,他们将需要上述代码)。

And now you can use jQuery throughout your site! (as long as the pages extend your basepage. If they don't, they will need the above piece of code in their html.)

在我的本地机器上我还没有尝试实际部署我的网站,所以我希望这仍然可以工作。

This works for me while working on my local machine. I haven't tried actually deploying my site yet, so I hope this will still work.

这篇关于如何使用jQuery设置Django网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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