【问题标题】:CSS File not being detected未检测到 CSS 文件
【发布时间】:2021-07-09 16:55:19
【问题描述】:

附加到我的 python 代码的 CSS 文件不起作用,我似乎无法弄清楚原因,因为没有错误,而且我尝试了多种方法。

结构:

app{
    |app
        |templates
            |public
                |index.html
    |env
    |static
        |css
            |styles.css

我已将它附加到 index.html <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">

在我使用的python文件中

@app.route("/")
def index():
        return render_template("public/index.html")

一切正常,但 CSS 在页面上不起作用: style.css

body {
    color: red;
}

【问题讨论】:

    标签: python html css python-3.x flask


    【解决方案1】:

    styles.css 还是style.css? 检查css文件名!

    如果这是您的问题而不是您的主代码中的拼写错误,请尝试使用 Ctrl + Shift + R 重新加载您的页面。

    【讨论】:

    • 名称都是styles.css,我的评论有误。每次在调试模式下更新页面时,我都会重新加载页面。还是不行。
    • 我在这里看不到任何错误。你确定html文件的head标签里面有<link rel="stylesheet"...>标签吗?
    【解决方案2】:

    根据您的项目结构,static 子文件夹位于 app 文件夹之外。考虑将其添加到与templates 相同的位置。

    project
     | -- app
           | -- templates/
           | -- static/
    

    您的<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}"> 应该可以工作。

    【讨论】:

      【解决方案3】:

      或许尝试将文件名参数放入:`filename='/static/css/styles.css',看看是否有效。

      【讨论】:

      • 这似乎不起作用。虽然我在查看 chrome 源代码时注意到,它似乎是在生成自己的文件,而不是检测文件夹中已经存在的 styles.css 文件。 gyazo.com/15ec94b4542957f1c51ee815caa16d2e
      • 很奇怪。这应该有助于 [链接] (stackoverflow.com/questions/22259847/…)。我认为您缺少type="text/css",这似乎是问题所在。
      • 我已经尝试过了,但未能成功。
      猜你喜欢
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2016-05-25
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多