已经坐了一个小时,无法弄清楚为啥找不到 /js/bootstrap.min.js
Posted
技术标签:
【中文标题】已经坐了一个小时,无法弄清楚为啥找不到 /js/bootstrap.min.js【英文标题】:Have been sitting on this for an hour and cant figure our why /js/bootstrap.min.js is not found已经坐了一个小时,无法弄清楚为什么找不到 /js/bootstrap.min.js 【发布时间】:2021-06-05 02:47:21 【问题描述】:我在 base.html 中获得了以下代码,我正在尝试制作引导启动模板。我已经从引导程序复制粘贴,但运行时出现以下错误:/js/bootstrap.min.js。你能告诉我有什么问题吗?谢谢!
<!DOCTYPE html>
<html lang="en">
<head>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
% if title %
<title>Django Blog - title</title>
% else %
<title>Django Blog </title>
% endif %
</head>
<body>
<div class="container">
% block content %% endblock %
!-- jQuery (necessary for Bootstrap's javascript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</div>`enter code here`
</body>
</html>
【问题讨论】:
如果您已经为静态文件正确配置了设置和 url,写入src="% static 'js/bootstrap.min.js' %"
(必须首先在模板开头写入 % load static %
)和类似的其他静态文件就可以了。跨度>
【参考方案1】:
<script>
s 路径 js/bootstrap.min.js
(以及样式表的 css/bootstrap.min.css
)都表示相对路径。
看到您使用这个 html 文件作为模板,它很可能被不同的页面重用,生成用于在不同的 URL 上显示。
假设您的项目至少在这些不同的地址上提供一些内容:
-
http://www.example.org/index.html
http://www.example.org/storelocations/london.html
http://www.example.org/products/listing.html
http://www.example.org/products/details/baseballcap.html
如果您在模板中将<script>
路径指定为js/bootstrap.min.js
,webbbrowser 会将其理解为相对于当前页面路径的路径,因此会尝试在以下位置下载它:
-
http://www.example.org/js/bootstrap.min.js
http://www.example.org/storelocations/js/bootstrap.min.js
http://www.example.org/products/js/bootstrap.min.js
http://www.example.org/products/details/js/bootstrap.min.js
注意,这意味着每个页面的位置不同。 这通常不是您想要的(出于多种原因)。
如果您在模板中将<script>
路径指定为/js/bootstrap.min.js
,webbbrowser 会将其理解为绝对路径,因此会尝试在以下位置下载它:
-
http://www.example.org/js/bootstrap.min.js
http://www.example.org/js/bootstrap.min.js
http://www.example.org/js/bootstrap.min.js
http://www.example.org/js/bootstrap.min.js
请注意,现在在每种情况下它的 URL 都是相同的,这通常是一件好事,而且几乎可以肯定是您想要的。
但是,这可能不是您选择存储bootstrap.min.js
文件的实际路径。
结合上面的例子,这些变化可能会说明这个概念:
如果您在模板中将<script>
路径指定为/bootstrap-files/js/bootstrap.min.js
:
-
http://www.example.org/bootstrap-files/js/bootstrap.min.js
http://www.example.org/bootstrap-files/js/bootstrap.min.js
http://www.example.org/bootstrap-files/js/bootstrap.min.js
http://www.example.org/bootstrap-files/js/bootstrap.min.js
如果您在模板中将<script>
路径指定为/bootstrap.min.js
:
-
http://www.example.org/bootstrap.min.js
http://www.example.org/bootstrap.min.js
http://www.example.org/bootstrap.min.js
http://www.example.org/bootstrap.min.js
【讨论】:
感谢您的回复!所以 js/bootstrap.min.js -> /js/bootstrap.min.js 和 css/bootstrap.min.css-> /css/bootstrap.min.css?因为我刚试了八次,还是不行。。 谢谢你,我已经根据你的建议进行了修改,也改变了j查询脚本的来源,它工作了!【参考方案2】:替换
<script src="js/bootstrap.min.js"></script>
与
% load static %
<script src="% static 'js/bootstrap.min.js' %"></script>
请注意,您必须在 settings.py 中设置静态文件设置
【讨论】:
以上是关于已经坐了一个小时,无法弄清楚为啥找不到 /js/bootstrap.min.js的主要内容,如果未能解决你的问题,请参考以下文章