【发布时间】:2019-04-16 13:41:00
【问题描述】:
自从我完成了 Django 网站的后端后,我开始添加一些样式。 我的 html 文件已显示,但似乎没有 css 链接到它们但是当我在 Firefox 中显示源代码时路径是正确的。
mywebsite/
----blog/
----mywebsite/
----static/
--------css/
------------struct.css
----templates/
--------layouts/
--------errors/
------------404.html
--------html/
------------struct.html
Django 版本:2.1.7
settings.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
# other apps...
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
struct.html:
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0;">
<link rel="stylesheet" href="{% static 'css/struct.css' %}">
<title>test</title>
</head>
<body>
<p>test</p>
</body>
</html>
struct.css:
p{
color:red:
}
body{
background-color:black:
}
【问题讨论】:
-
你能把
settings.py的整个路径打印出来吗?print(os.path.join(BASE_DIR, "static")) -
/home/user/RedPillers/red_pillers/static
-
red_pilers 是我的项目
-
这听起来很傻,但是您是否尝试过设置 DEBUG=True (本地)当然。因为你还没有设置 STATIC_ROOT 并且你没有运行 collectstatic 可能就是这样。如果是,那么您需要遵循文档。
-
刚刚看到您对以下答案的评论。当您开发 (DEBUG=True) 时,您所做的工作。但是,当您打开 DEBUG=False 时,您需要设置 STATIC_ROOT 作为文档所说的docs.djangoproject.com/en/2.2/howto/static-files/#deployment