Python 为什么赢了';Django在我已经使用static时加载我的CSS文件吗?
我正在尝试创建一个网站,但我不知道为什么我的一些CSS不能在页面上工作。我遵循老师给我的格式,他的工作方式让我明白为什么我的不会 在项目的URL.py中:Python 为什么赢了';Django在我已经使用static时加载我的CSS文件吗?,python,html,css,django,Python,Html,Css,Django,我正在尝试创建一个网站,但我不知道为什么我的一些CSS不能在页面上工作。我遵循老师给我的格式,他的工作方式让我明白为什么我的不会 在项目的URL.py中: from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('system/', include ('system.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在HTML文件中:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">
当I
python manage.py runserver
时,页面背景仍然是白色的,文本也是白色的,因此我必须阻止它们才能看到它。我试图执行collectstatic,但它给出了一个警告,它将覆盖现有文件,所以我取消了它。我的老师也没有教我们静态命令。我在这里做错了什么?我记得有一个类似的问题,尽管我已经有一段时间没有解决了。我相信我最终的解决方案是将下面的代码添加到我的设置文件中。这还取决于您如何设置文件树,因为我认为静态URL指的是目录名,尽管我必须仔细检查
看
然后使用命令:python manage.py collectstatic
PS:你从来没有告诉过你的CSS文件保存在哪里。因此,这可能不起作用
collectstatic
仅用于生产环境,请检查您的静态设置当我使用您的代码时,它给出了一个错误名称“os”未定义
因此我尝试了静态文件\u DIRS=[BASE_DIR/“static”、'/ssip/hotel/hotel/static/,]
,它起了作用。谢谢你的提示:)没问题,很高兴它起作用了!作为将来的参考,操作系统可以直接导入。它与python一起提供(无需安装pip)。(导入操作系统)
.body{
background-color: yellow;
}
.title
{
color: #000;
font-family: Arial Black;
position: absolute;
top: 0;
left: 4%;
}
.list
{
color: #000;
font-family: Arial;
position: absolute;
top: 10%;
left: 10%;
}
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")