Python 从外部css(django)加载背景图像时出现问题
我在Django上通过外部CSS加载图像时遇到问题。它可以很好地使用内联CSS,但我想了解为什么它不能使用外部CSS。如有任何帮助,将不胜感激: 我的树: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我使
├───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');
}