【发布时间】:2017-10-03 07:18:18
【问题描述】:
我不明白为什么我的静态文件(css 和图像)在我的 Django 项目中正确加载,当我从 html 标记加载静态文件但当我想通过 css 背景设置背景图像时它工作正常: url('images/mypic.png') (我也试过 url('../images/mypic.png')) 但它根本不起作用。
图片加载在img标签中,但没有加载到我调用css的div中
{% 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>
还有我的 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;
}
项目结构是 我的项目 -我的网站 -静止的 -css 样式.css -图片 logo_aasev.png -模板 base.html 初始化.py 设置.py 网址.py wsgi.py 管理.py 等等……
我是否错过了 Django 设置中的某些内容?我觉得奇怪的是静态文件加载正确。
【问题讨论】:
-
您的浏览器控制台有错误吗?
-
[05/May/2017 01:30:51] "GET /fr/ HTTP/1.1" 200 988 [05/May/2017 01:30:51] "GET /static/css/ styles.css HTTP/1.1" 200 514 [05/May/2017 01:30:51] "GET /static/images/logo_aasev.png HTTP/1.1" 304 0 这就是我加载页面时发生的情况,实际上没有错