为什么 css 文件不应用于 html 文件 - Django

Why doesen't css file apply on html file - Django

我有一个问题,我的 .css 文件不适用于 Django 中的 .html 文件。但是,例如,当我输入 . .html 文件中的一些 class 名称,pycharm 让我完成 class 名称。所以,我认为它的链接是正确的。

无论如何,我把我的设置文件:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

运行 python manage.py collectstatic 成功并将我的 .css 文件添加到 .html 使用命令:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'base.css' %}"/>

在我的项目中,我有这样的东西:

-website
   -static
        -admin (generated when I run the command above)
        -images
        -base.css
    -templates
        -navbar.html
        -base.html
     -app1
     -app2
     -etc

出于测试目的,我只是将简单的 div 放在 .html 文件的头部:

 <div class="example">jvhgbhjgf</div>

并且在 .css 文件中:

.example { background-color: red; border-radius: 15px;}

在您的 urls.py 文件中添加以下代码:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns [ ... ]

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

更改您的 settings.py 文件,如下所示:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR), 'static', 'static_dirs'),
    )

和目录树:

-website
-static
    -static_root
        -admin (generated when I run the command above)
    -static_dirs
        -images
        -base.css
-templates
    -navbar.html
    -base.html
 -app1
 -app2
 -etc

在 static_dirs 文件夹中添加 base.css 文件。