[英]Django cant find static files
I am trying to build a forum with Django.我正在尝试使用 Django 建立一个论坛。 I want to use an animated background with particles.js.
我想使用带有particle.js 的动画背景。 When I write this code:
当我写这段代码时:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Particles login</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="particles-js">
</div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
particlesJS.load('particles-js', 'particles.json', function(){
console.log('particles.json loaded...');
});
</script>
</body>
</html>
and run it on live server I get what I want.并在实时服务器上运行它,我得到了我想要的。 However, I tweaked the code for Django:
但是,我调整了 Django 的代码:
<!DOCTYPE html>
<html lang=en>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
{% include 'snippets/base_css.html' %}
{% include 'snippets/header.html' %}
<!-- Body -->
<style type="text/css">
.main{
min-height: 100vh;
height: 100%;
}
#particles-js{
height: 100%;
color:turquoise;
}
</style>
<div class="main">
<div id="particles-js">
</div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"></script>
<script>
particlesJS.load('particles-js', 'particles.json', function(){
console.log('particles.json loaded...');
});
</script>
{% block content %}
{% endblock content %}
</div>
</div>
<!-- End Body -->
{% include 'snippets/footer.html' %}
</body>
</html>
When I run this with Django.当我用 Django 运行它时。 I get the following error even though the locations of those files are exactly the same:
即使这些文件的位置完全相同,我也会收到以下错误:
Not Found: /style.css
[28/May/2021 00:21:59] "GET /style.css HTTP/1.1" 404 2263
Not Found: /particles.json
[28/May/2021 00:21:59] "GET /particles.json HTTP/1.1" 404 2278
Can someone please enlighten me about this issue?有人可以请教我这个问题吗? Should I add something to static files in settings.py?
我应该在 settings.py 中的 static 文件中添加一些内容吗?
First of all, in settings.py , add this:首先,在settings.py 中,添加:
STATICFILES_DIRS = [
"<PATH_TO_YOUR_STATIC_DIR>"
]
Then at the beginning of your HTML file, write {% load static %}
然后在你的 HTML 文件的开头,写
{% load static %}
After that, wherever you want to reference a static file, do so by making use of the following syntax: {% static 'PATH_TO_STATIC_FILE' %}
之后,无论您想在何处引用 static 文件,请使用以下语法:
{% static 'PATH_TO_STATIC_FILE' %}
For example, <link rel="stylesheet" href="{% static 'css/style.css' %}" />
Do this if you're keeping your style.css file in "css" directory.例如,如果您将 style.ZC7A62
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
文件保留在“css”目录中,请执行此操作。 If not, then directly write {% static 'style.css' %}
如果没有,那么直接写
{% static 'style.css' %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.