首页 > 解决方案 > 链接到有效的外部 css,但显示未设置样式

问题描述

我的项目论坛将其文件构建为:

forum
├── __init__.py
├── settings.py
├── static
│   └── css
│       └── bootstrap.min.css
├── templates
│   └── index.html
├── urls.py
└── wsgi.py

我将“bootstrap.min.css”链接到index.html喜欢:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Forum Home Page</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css" />
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-md-12">
          <h2>Coding Journey</h2>
        </div>
      </div>

      <div class="row">
        <div class="col-xs-12 col-md-2">Concepts
          <button type="button" class="btn btn-success">Submit</button>
        </div>
        <div class="col-xs-12 col-md-2">Reading</div>
        <div class="col-xs-12 col-md-2">Coding</div>
      </div>

    </div> <!--container-->

  </body>
</html>

但是,当index.html在浏览器中打开时,它根本没有样式。

我的操作有什么问题?

在此处输入图像描述

设置.py

STATIC_URL = '/static/'

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

标签: cssdjango

解决方案


查看您的设置文件,这可能是因为您仅将一个字符串作为字符串列表/元组传递....
我认为您的意思是

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),  # notice the comma here
)

这是 Python 的问题之一,您需要添加显式逗号来区分元组和括号


推荐阅读