Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从外部css(django)加载背景图像时出现问题_Python_Html_Css_Django - Fatal编程技术网

Python 从外部css(django)加载背景图像时出现问题

Python 从外部css(django)加载背景图像时出现问题,python,html,css,django,Python,Html,Css,Django,我在Django上通过外部CSS加载图像时遇到问题。它可以很好地使用内联CSS,但我想了解为什么它不能使用外部CSS。如有任何帮助,将不胜感激: 我的树: ├───migrations │ └───__pycache__ ├───static │ └───website │ ├───css │ ├───img │ └───js ├───templates │ └───website └───__pycache__ CSS: HTML: p.S我使

我在Django上通过外部CSS加载图像时遇到问题。它可以很好地使用内联CSS,但我想了解为什么它不能使用外部CSS。如有任何帮助,将不胜感激:

我的树:

├───migrations
│   └───__pycache__
├───static
│   └───website
│       ├───css
│       ├───img
│       └───js
├───templates
│   └───website
└───__pycache__
CSS:

HTML:

p.S我使用的是localhost,图像确实通过URL加载:

替换:

.body {
  background: url('/static/website/img/Racoon.jpg');
}

。/
表示父目录。您首先返回您的
static/website
文件夹,然后进入
img
文件夹并找到您的图像


如果仍不工作,请清除浏览器历史记录并重试。

Django静态文件仅在html文件中加载。在其他文件(如外部css文件)中不工作。这是django模板规则。替换为.body{background:url('../img/Racoon.jpg');}谢谢,我不知道
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'website/css/main.css' %}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Russo+One&display=swap" rel="stylesheet">
{% block title %}
<title> Home </title>
{% endblock %}
{% block content %}
<h1> Home </h1>
<p> Homepage of the website </p>
{% endblock %}
STATIC_URL = '/static/'
.body {
  background: url('/static/website/img/Racoon.jpg');
}
.body {
  background: url('../img/Racoon.jpg');
}