[英]Django Static Files - CSS File doesnt load
I'm learning Django.我正在学习 Django。 I am trying to load a static css file, but it doesn't work.
我正在尝试加载静态 css 文件,但它不起作用。 That's part of my
base.html
:这是我的
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>
and that's part of my settings.py
file:这是我的
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")
Where did I make mistake ?我在哪里犯错了?
I have already used python manage.py collectstatic
in command prompt我已经在命令提示符下使用了
python manage.py collectstatic
edit: I see that I declareted STATICFILES_DIRS list two times.编辑:我看到我两次声明了 STATICFILES_DIRS 列表。
How does href
look in the browser? href
在浏览器中的外观如何?
I would guess there is a missing /
in the href
path.我猜想在
href
路径中缺少/
。
<link rel="stylesheet" type="text/css" href="{% static '/HomePage/style.css' %}">
Try to put a dot in the path.尝试在路径中添加一个点。
like ./HomePage/style.css
像
./HomePage/style.css
It looks like it is resolving correctly because your source has href="/static/HomePage/style.css"
看起来它正在正确解析,因为您的源具有
href="/static/HomePage/style.css"
What happens when you open localhost:8000/static/HomePage/style.css
in your browser.在浏览器中打开
localhost:8000/static/HomePage/style.css
会发生什么。 Do you get a 404?你有404吗?
And, to be sure, what happens with localhost:8000/HomePage/static/HomePage/style.css
而且,可以肯定的是,
localhost:8000/HomePage/static/HomePage/style.css
会发生什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.