Boi*_*dot 5 css django image django-staticfiles
我使用此模板在django项目中创建了主页,但是我无法正确显示背景图片(bg.jpg)
背景图片在css文件中用作跟随项:
.wrapper.style3 {
background-color: #000;
color: #bfbfbf;
background-image: url(/media/usr/path_to_project/project_name/home/static/images/bg.jpg);
background-size: cover;
background-attachment: fixed;
background-position: center;
position: relative;
}
Run Code Online (Sandbox Code Playgroud)
我读了这些话题
并尝试了所有解决方案,但似乎都不起作用。
我的项目树就像
project_name
- home
- static
--home
---style.css
--images
---bg.jpg
- templates
-- home
---base.html
---home_template.html
Run Code Online (Sandbox Code Playgroud)
在style.css文件中,我尝试了以下操作
background-image: url(/media/usr/path_to_project/project_name/home/static/images/bg.jpg);
background-image: url("/media/usr/path_to_project/project_name/home/static/images/bg.jpg");
background-image: url(../images/bg.jpg);
background-image: url("../images/bg.jpg");
background-image: url("{% static 'images.bg.jpg' %});
Run Code Online (Sandbox Code Playgroud)
在我的base.html模板中,我有:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'home/style.css' %}"/>
Run Code Online (Sandbox Code Playgroud)
在我的home_template.html中
{% block body %}
{% load staticfiles %}
<section id="two" class="wrapper style3">
<div class="inner">
<header class="align-center">
<p>Nam vel ante sit amet libero scelerisque facilisis eleifend vitae urna</p>
<h2>Morbi maximus justo</h2>
</header>
</div>
</section>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我的static / images目录中还有其他图像,这些图像在带有内联样式的模板中可以正常显示,例如:
<div class="image fit">
<img src="{% static 'images/pic03.jpg' %}" alt="" />
</div>
Run Code Online (Sandbox Code Playgroud)