【问题标题】:Static CSS file not loading in Django framework静态 CSS 文件未在 Django 框架中加载
【发布时间】:2017-05-22 09:10:33
【问题描述】:

我在 localhost 上运行,但样式表不起作用

这是我的项目设置:

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

我的设置.py:

STATICFILES_DIRS = [
    "/static/",
]

STATIC_URL = '/static/'

index.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>

这是我的服务器输出的内容:"GET /static/css/style.css HTTP/1.1" 404 1652

怎么了?

【问题讨论】:

    标签: python django python-2.7 static django-templates


    【解决方案1】:

    好的,这个更改似乎解决了它:

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

    【讨论】:

      【解决方案2】:

      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>
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,因为我在 Django 中的设置文件没有以下代码行,我添加了这些代码来解决问题

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

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

        【讨论】:

          猜你喜欢
          • 2020-04-28
          • 2022-01-20
          • 2020-08-31
          • 1970-01-01
          • 2020-05-12
          • 1970-01-01
          • 2021-11-07
          • 2019-08-31
          相关资源
          最近更新 更多