javascript Django静态js文件不起作用

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

Django Static js files not working

javascriptdjangodjango-templatesdjango-staticfiles

提问by Ayub Khan

Well my template code.

那么我的模板代码。

<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript" src="{% static 'website/staffaddproblem.js' %}"></script>
    <title>Dashboard</title>
    <link href="{% static 'website/style.css' %}" rel="stylesheet" type="text/css"/>
</head>
<body>
        body
</body>
<html>

As you can see it has a css file. which is working properly, but java script file does not work. when i load the page its source looks like this. enter image description here

如您所见,它有一个 css 文件。它工作正常,但java脚本文件不起作用。当我加载页面时,它的源代码如下所示。 在此处输入图片说明

When i click the link it loads the file too its there its linking fine but its not working . there is no problem in code js code working fine when written in same file.

当我单击该链接时,它也会加载文件,它的链接正常,但无法正常工作。编写在同一个文件中时,代码 js 代码工作正常没有问题。

回答by Abhyudit Jain

Have you changed the settings.pyfile to include staticroot?

您是否settings.py已将文件更改为包含 staticroot?

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Here's a part of my template which works with both jsand cssfiles.

这是我的模板的一部分,它适用于jscss文件。

{% load static %}
<html>
<head>
<link type="text/css" rel="stylesheet" href="{% static 'css/materialize.min.css' %}">
<link type="text/css" rel="stylesheet" href="{% static 'css/stylesheet.css' %}">
<script type="text/javascript" src="{% static 'js/jquery-1.11.3.min.js' %}"></script>
<title>

The directories are as follows:

目录如下:

 - static
     -js
         - jquery-1.11.3.min.js
     -css     
         - stylesheet.css
         - materialize.min.css
 - templates
     -index.html