Django静态文件-CSS文件不加载



我正在学习Django。我正在尝试加载一个静态css文件,但它不起作用。这是我base.html:的一部分

{% load static %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- css -->

<link rel="stylesheet" type="text/css" href="{% static 'HomePage/style.css' %}">

</head>

这是我的settings.py文件的一部分:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
'/var/www/static/',
]
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "static_root")

我哪里搞错了?

我已经在命令提示符中使用了python manage.py collectstatic

编辑:我看到我背诵了两次STATICILES_DIRS列表。

href在浏览器中的外观如何?

我猜href路径中缺少一个/

<link rel="stylesheet" type="text/css" href="{% static '/HomePage/style.css' %}">

尝试在路径中放置一个点。

./HomePage/style.css

由于您的源具有href="/static/HomePage/style.css",因此看起来它正在正确解析

当您在浏览器中打开localhost:8000/static/HomePage/style.css时会发生什么。你有404吗?

当然,localhost:8000/HomePage/static/HomePage/style.css会发生什么

最新更新