简体   繁体   English

静态 CSS 文件未在 Django 框架中加载

[英]Static CSS file not loading in Django framework

I'm running on localhost but stylesheet isn't working我在本地主机上运行,​​但样式表不起作用

Here is my project setup:这是我的项目设置:

-main
   -main
   -home
      -templates
         -home
             -index.html
   -static
      -css
         -style.css

My settings.py:我的设置.py:

STATICFILES_DIRS = [
    "/static/",
]

STATIC_URL = '/static/'

index.html:索引.html:

{% load staticfiles %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>My Home Page</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
<p>My Content</p>
</body>
</html>

Here is what my server outputs: "GET /static/css/style.css HTTP/1.1" 404 1652这是我的服务器输出的内容: "GET /static/css/style.css HTTP/1.1" 404 1652

What is wrong?怎么了?

设置是STATICFILES_DIRS ,而不是 STATICFILES_DIR。

Okay this change seemed to fix it:好的,这个更改似乎解决了它:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/static/',
]

index.html should be like this: index.html应该是这样的:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
{% load staticfiles %}
<html lang="en">
<head>
    <title>My Home Page</title>
    <link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
    <p>My Content</p>
</body>
</html>

STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),] STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),]

will work!将工作!

I was having the same issue because my settings file in Django didn't have the following lines of code, which I added to solve the problem我遇到了同样的问题,因为我在 Django 中的设置文件没有以下代码行,我添加了这些代码来解决问题

STATICFILES_DIRS = [
    BASE_DIR / "static",
    '/var/www/static/',
]

see the static files documentation here: https://docs.djangoproject.com/en/3.1/howto/static-files/在此处查看静态文件文档: https : //docs.djangoproject.com/en/3.1/howto/static-files/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM