【问题标题】:Django css isn't working, even though the css file is foundDjango css 不工作,即使找到 css 文件
【发布时间】:2018-10-25 20:37:38
【问题描述】:

所以在我的 html 中我有这个链接

<link href="/static/css/style.css" rel="stylesheet">

我已经设置了我所有的静态文件: 设置.py:

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

urls.py:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我已经运行了 python manage.py collectstatic

因此,当我使用链接加载此页面时,引导 css 加载并且它没有说它找不到 style.css 文件,所以我很确定它不是我的静态目录的问题,因为文件正在注册,当它找不到文件时,它会在加载页面时在命令 shell 中显示。

这是 style.css 外观的 sn-p。我不会向您展示整个内容,因为它很长:

html,
body,
header,
.view {
height: 100%;
 }

.icon-1 {
  font-size: 75px;
} 

@media (max-width: 740px) {
html,
body,
header,
.view {
  height: 1000px;
}
}
@media (min-width: 800px) and (max-width: 850px) {
html,
body,
header,
.view {
  height: 600px;
}
}
@media (min-width: 800px) {
.music-margin-top {
   margin-top: 140% !important;
}

.music-just2{
  left: .4%;
}

.music-just4{
  left: .3%;
}

所以基本上我的自定义 css 都没有显示出来,但是在加载页面时似乎找到了该文件。此外,如果我将 style.css 中的所有内容放入我的 html 文件的样式括号中,它加载得非常好。对我做错了什么有什么想法吗?

【问题讨论】:

    标签: html css django static styles


    【解决方案1】:

    所以我发现如果我将 css 添加到一个名为 style.min.css 的文件中并链接它,它就可以工作。谁能给我解释一下这是为什么?

    【讨论】:

    • 您的静态 url 为 /static/,这意味着您的所有静态文件都在静态目录中。现在我猜,你的静态目录中有一个名为 css 的文件夹,在 css 中有你需要的文件。因此,您想要的路径是 static/css/style.css
    • 原因是你有另一个同名的CSS文件 style.css 可能在另一个应用程序的静态目录中,使两者相互冲突。因此,通过重命名两者之一将解决您已经看到的问题。但这里的重点是,它不必是 style.min.css,任何重命名都可以。
    【解决方案2】:

    只需 STATIC_ROOT 中的 BASE_DIR 就足够了 = os.path.join(BASE_DIR, 'static')

    【讨论】:

      猜你喜欢
      • 2022-08-15
      • 2022-01-18
      • 2016-04-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多