如何使用 jQuery 设置 Django 网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12031825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up Django website with jQuery
提问by SaiyanGirl
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
我不知道为什么,但我不知道如何在我的网站上激活 jQuery。所有做教程“在 Django 上启动 jQuery”的网站都是从已经在他们网站上运行的 JQuery 开始的。无论如何,我没有下载它并将其放在我的文件夹中的某个地方,而是决定采用作弊方式并使用 Google 的。因此,在我的基页中,我有以下代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
</script>
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?
一切都很好,直到我不得不添加一些库。如何设置我自己的 jQuery 以便它与我的模板一起使用?
回答by SaiyanGirl
In the end, it turned out to be very easy. This is all one has to do:
最后,它变得非常容易。这是一个人必须做的:
First, make a folder named static
inside your folder app:
首先,static
在您的文件夹应用程序中创建一个名为的文件夹:
mySite
---mytemplates
---mySite
---myApp
------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.
然后从他们的网站下载的jQuery这里。您单击“下载”,它会将您带到包含所有代码的不同页面。左键单击代码并选择“另存为...”。将其保存在static
文件夹中。
Inside your settings.py
file make sure that django.contrib.staticfiles
is under INSTALLED_APPS
(it is there by default).
在您的settings.py
文件中,确保它django.contrib.staticfiles
位于INSTALLED_APPS
(默认情况下)。
Lastly, inside your base page have
最后,在您的基页内有
<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
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.)
现在您可以在整个站点中使用 jQuery!(只要这些页面扩展了您的基本页面。如果它们不扩展,他们将需要在其 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.
在我的本地机器上工作时,这对我有用。我还没有尝试过实际部署我的网站,所以我希望这仍然有效。
回答by odinho - Velmont
You can also just pip install django-jquery
, and put jquery
in INSTALLED_APPS
. Of course, you need to run collectstatic
as always after. :)
您也可以只pip install django-jquery
,并把jquery
在INSTALLED_APPS
。当然,您需要collectstatic
像往常一样运行。:)
That way you can just have it in your requirements.txt
file if you use one.
这样你就可以在你的requirements.txt
文件中使用它。
回答by Snowrabbit
You may also use the python package django-static-jquery
. For the moment, it comes with the more recent jquery libraries.
您也可以使用 python 包django-static-jquery
。目前,它带有更新的 jquery 库。
pip install django-static-jquery=='version'
. Package & installation info here https://pypi.python.org/pypi/django-static-jquery/
pip install django-static-jquery=='version'
. 此处的软件包和安装信息https://pypi.python.org/pypi/django-static-jquery/
Looks like the other package django-jquery
not been updated in a while.
看起来另一个包django-jquery
有一段时间没有更新了。
回答by Marc Guvenc
I put my jquery file in the same place as SaiyanGirl.
我把我的 jquery 文件和 SaiyanGirl 放在同一个地方。
In my setting I had this by default in INSTALLED_APPS: 'django.contrib.staticfiles',
在我的设置中,我默认在 INSTALLED_APPS 中有这个:'django.contrib.staticfiles',
At the top of my html page I have this:
在我的 html 页面顶部,我有这个:
{% load static %}
<!DOCTYPE html>
I have this in my head tag:
<script type="text/javascript" src="{% static 'jquery-3.5.0.js' %}"></script>
我的头标签中有这个:
<script type="text/javascript" src="{% static 'jquery-3.5.0.js' %}"></script>
This is how I tested it (in the head tag too):
这就是我测试它的方式(也在 head 标签中):
<script type="text/javascript">
window.onload = function() {
if (window.jQuery) {
alert('jQuery is loaded');
} else {
alert('jQuery is not loaded');
</script>