[英]Not Found: /style.css/ , django
我正在尝试在 Django 上创建我自己的网站,但有些问题阻止了我,我无法自己解决。 我想创建一个侧边栏。 我找到了一个带有 css 和 HTML 代码的网站。
样式.css:
@import url('https://fonts.googleapis.com/css?family=Montserrat:600|Open+Sans:600&display=swap');
*{
margin: 0;
padding: 0;
text-decoration: none;
}
.sidebar{
position: fixed;
width: 240px;
left: -240px;
height: 100%;
background: #1e1e1e;
transition: all .5s ease;
}
.sidebar header{
font-size: 28px;
color: white;
line-height: 70px;
text-align: center;
background: #1b1b1b;
user-select: none;
font-family: 'Montserrat', sans-serif;
}
.sidebar a{
display: block;
height: 65px;
width: 100%;
color: white;
line-height: 65px;
padding-left: 30px;
box-sizing: border-box;
border-bottom: 1px solid black;
border-top: 1px solid rgba(255,255,255,.1);
border-left: 5px solid transparent;
font-family: 'Open Sans', sans-serif;
transition: all .5s ease;
}
a.active,a:hover{
border-left: 5px solid #b93632;
color: #b93632;
}
.sidebar a i{
font-size: 23px;
margin-right: 16px;
}
.sidebar a span{
letter-spacing: 1px;
text-transform: uppercase;
}
#check{
display: none;
}
label #btn,label #cancel{
position: absolute;
cursor: pointer;
color: white;
border-radius: 5px;
border: 1px solid #262626;
margin: 15px 30px;
font-size: 29px;
background: #262626;
height: 45px;
width: 45px;
text-align: center;
line-height: 45px;
transition: all .5s ease;
}
label #cancel{
opacity: 0;
visibility: hidden;
}
#check:checked ~ .sidebar{
left: 0;
}
#check:checked ~ label #btn{
margin-left: 245px;
opacity: 0;
visibility: hidden;
}
#check:checked ~ label #cancel{
margin-left: 245px;
opacity: 1;
visibility: visible;
}
@media(max-width : 860px){
.sidebar{
height: auto;
width: 70px;
left: 0;
margin: 100px 0;
}
header,#btn,#cancel{
display: none;
}
span{
position: absolute;
margin-left: 23px;
opacity: 0;
visibility: hidden;
}
.sidebar a{
height: 60px;
}
.sidebar a i{
margin-left: -10px;
}
a:hover {
width: 200px;
background: inherit;
}
.sidebar a:hover span{
opacity: 1;
visibility: visible;
}
}
侧边栏.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Responsive Sidebar Menu</title>
<link rel="stylesheet" href='style.css'/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
</head>
<body>
<input type="checkbox" id="check">
<label for="check">
<i class="fas fa-bars" id="btn"></i>
<i class="fas fa-times" id="cancel"></i>
</label>
<div class="sidebar">
<header>My Menu</header>
<a href="#" class="active">
<i class="fas fa-qrcode"></i>
<span>Dashboard</span>
</a>
<a href="#">
<i class="fas fa-link"></i>
<span>Shortcuts</span>
</a>
<a href="#">
<i class="fas fa-stream"></i>
<span>Overview</span>
</a>
<a href="#">
<i class="fas fa-calendar"></i>
<span>Events</span>
</a>
<a href="#">
<i class="far fa-question-circle"></i>
<span>About</span>
</a>
<a href="#">
<i class="fas fa-sliders-h"></i>
<span>Services</span>
</a>
<a href="#">
<i class="far fa-envelope"></i>
<span>Contact</span>
</a>
</div>
</body>
</html>
如果我在任何代码段中启动它,或者如果我只是在 google 中从 pycharm 运行 sidebar.html,那么所有这些都有效,但是当我启动我的网站时,它不起作用并给出错误:未找到:/style.css/ Both文件在同一个目录下。
很容易修复,但您需要更多地研究 Django。
首先,这个标签<link rel="stylesheet" href=style.css/>
永远不会工作。 标签在几个级别上写错了。 最大的问题是位置是相对的, style.css
的使用被假定为与调用 page/script/etc 的目录级别相同。 问题是 wsgi.py 或类似文件实际上是“运行”站点的“根”,并且不知道“style.css”在哪里存在,而且 deos 也不关心。
样式、图像、js 等都作为静态资产存储在 Django 中。 此文件夹使用static
标签“提供”,该标签将根据您的本地开发和生产配置转换为正确的路径。 看看这里: https : //docs.djangoproject.com/en/3.1/howto/static-files/
我不会解释所有的细微差别,Django 站点做得更好。 相反,我将指出 Django 与静态站点或 PHP 之类的东西有何不同。 Django 是一个运行在 CGI (WSGI) 中的应用程序,可以说服务器上只有一个“路由”,所有数据都来自这个文件。 PHP 可以并且通常确实以文件+目录的方式提供数据。 在 PHP/静态场景中,与 Django 相比,文件的位置是稳定的。 在 Django 中,页面、url 和数据的提供方式都来自一个点。 这意味着与静态文件的关系将有所不同,而不是您可以或应该控制的。
Django 不希望您通过 CGI (WSGI) 提供静态文件,这会浪费 CPU 和资源,并且静态文件很慢。 所以他们有一个静态系统。 当在本地运行且配置设置正确且DEBUG=True
, static
关键字将结合您的配置转换为必要的本地路径。
在生产中,假定使用 CDN,在这种情况下, static
关键字将替换为 CDN 静态文件的路径 (URL)。
Django 使用的方法比 Wordpress 成熟得多,在那里使用 CDN 可能很棘手(我已经多次为 WP 编写自定义 CDN,但并不有趣)。
浏览上面的链接,正确设置您的配置并遵守规则。 Django 对你遵守规则很感兴趣,Deviation 只会带来痛苦。 近 10 年来,我一直主要是 Django 开发人员。 您遇到的问题一开始让我非常满意,但现在 CDN 和静态文件管理已成为第二天性,并且肯定比其他不太成熟的系统更有效率。
在您的“settings.py”上设置静态根。
你会想要将<link rel="stylesheet" href=style.css/>
更改为<link rel="stylesheet" href="style.css"/>
。 此外,它试图在其中找到您的style.css
文件的(whatever path to your sidebar.html)/sidebar.html/style.css
是这样的:( (whatever path to your sidebar.html)/sidebar.html/style.css
是(whatever path to your sidebar.html)/sidebar.html/style.css
。 我怀疑它位于那里。 如果它与sidebar.html
文件位于同一目录中,请尝试<link rel="stylesheet" href="./style.css"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.