javascript 如何在 Django 中使用 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11525994/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:31:43  来源:igfitidea点击:

How to use jQuery with Django?

javascriptjquerypythondjango

提问by user1530318

I am interested in using the jQuery tablesorterbut somehow am unable to. I have followed the instructions and have placed jquery.js and the tablesorter.js in the same folder as my templates (the folder where the html file is). Unfortunately, when trying to access the .js files, it keeps hitting a 404, which I'm assuming means that the files are not on the correct path. Any ideas for this fix?

我对使用 jQuery tablesorter感兴趣,但不知何故无法使用。我已按照说明将 jquery.js 和 tablesorter.js 放在与我的模板相同的文件夹中(html 文件所在的文件夹)。不幸的是,当尝试访问 .js 文件时,它不断遇到 404,我假设这意味着文件不在正确的路径上。对此修复有什么想法吗?

Does django have a special place to place these js files? (not in the templates folder?)

django 有专门的地方可以放置这些js文件吗?(不在模板文件夹中?)

<script type="text/javascript" src="jquery-latest.js"></script> 
<script type="text/javascript" src="jquery.tablesorter.js"></script>
<script type ="text/javascript">
    $(document).ready(function() 
    { 
       $("#myTable").tablesorter(); 
    } 
); 

The myTable is the same exact table as the one in the examples

myTable 与示例中的表完全相同

回答by machaku

Usually jquery, js, css, images and most of other static contents are handled as static files and not as django templates. Read managing static files docs.

通常 jquery、js、css、图像和大多数其他静态内容作为静态文件而不是 django 模板处理。阅读管理静态文件文档

回答by Clément Andraud

For jQuery, you can use Google API :

对于 jQuery,您可以使用 Google API:

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

And for Django, Did you configure a path for your scripts/médias etc... ? (in settings.py maybe ?)

对于 Django,您是否为脚本/媒体等配置了路径?(可能在 settings.py 中?)

回答by Chirael

To add to what others have written, if you want to make JQuery available throughout your site/app and you don't like external dependencies such as Google you can do the following:

要添加到其他人编写的内容中,如果您想让 JQuery 在整个站点/应用程序中可用,并且您不喜欢外部依赖项(例如 Google),您可以执行以下操作:

  1. Download the latest "compressed, production" JQuery at https://jquery.com/download/- for example, on the command line in your static directory (might be <projectname>/static): wget https://code.jquery.com/jquery-2.1.3.min.js
  2. Add a reference to load JQuery in your site's "base" template file (might be <projectname>/<appname>/templates/base.html) - for example: Add <script src="{% static 'jquery-2.1.3.min.js' %}"></script>in the <head>section
  3. Test the JQuery installation by adding something like the following to one of your templates:

    <script type="text/javascript"> $(document).ready( function(){ $("#jqtest").html("JQuery installed successfully!"); } ); </script> <p id="jqtest"></p>

  4. If necessary/usual after making template updates, restart your Django server, then load a page which uses the template with the test code

  1. https://jquery.com/download/下载最新的“压缩、生产”JQuery——例如,在你的静态目录(可能是 <projectname>/static)的命令行上:wget https://code.jquery.com/jquery-2.1.3.min.js
  2. 在站点的“基本”模板文件(可能是 <projectname>/<appname>/templates/base.html)中添加加载 JQuery 的引用 - 例如:<script src="{% static 'jquery-2.1.3.min.js' %}"></script><head>部分中添加
  3. 通过向模板之一添加类似以下内容来测试 JQuery 安装:

    <script type="text/javascript"> $(document).ready( function(){ $("#jqtest").html("JQuery installed successfully!"); } ); </script> <p id="jqtest"></p>

  4. 如果需要/通常在进行模板更新后,重新启动 Django 服务器,然后加载一个使用带有测试代码的模板的页面