I am facing an issue with my Django project where my static files (css and images) load properly when referenced in an html tag, but when I try to set a background image using CSS (background: url('images/mypic.png') or url('../images/mypic.png')), it doesn't work at all.
Interestingly, the image loads when used in an img tag but not when referenced in a div through CSS.
{% load static staticfiles cms_tags menu_tags sekizai_tags %}
<!doctype html>
<html>
<head>
<title>{% block title %}This is my new project home page{% endblock title %}</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="{% static 'css/styles.css' %}"/>
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
<div class="container">
<header>
<nav>
<div class="nav_logo"></div>
<ul class="nav">
{% show_menu 0 100 100 100 %}
</ul>
</nav>
</header>
<main class="container">
<img src="{% static 'images/logo_aasev.png' %}" alt="Photo de montagne" />
<div style="background-image: url('{% static "images/logo_aasev.png" %}')"></div>
{% block content %}{% endblock content %}
</main>
<footer>
</footer>
</div>
{% render_block "js" %}
</body>
</html>
As for my CSS:
* {
margin: 0;
padding: 0;
}
header {
width: 100%;
background-color: #000;
}
nav {
width: 90%;
margin: 0 auto;
padding: 10px 0px;
}
.nav {
padding-left: 0;
}
.nav li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
.container {
width: 940px;
margin: 0 auto
}
.content {
float: left;
width: 80%;
}
.sidebar {
float: left;
width: 20%;
}
.nav_logo {
background: white url("../images/logo_aasev.png") no-repeat right bottom;
}
Project directory structure: myproject -mysite -static -css styles.css -images logo_aasev.png -templates base.html init.py settings.py urls.py wsgi.py manage.py etc...
Could there be a configuration issue in Django settings? It's puzzling that static files load correctly but the background image does not.