【问题标题】:Flask-webage background image doesnt workFlask-网页背景图像不起作用
【发布时间】:2015-07-16 21:47:23
【问题描述】:

下面是一个简单的html代码

index.html

<html>
<head>
<title>Webpage</title>
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" >
<link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="sylesheet">
</head>
<body>
<style type=text/css>
    background-image: url("static/images/back.png");
</style>
<p>Does the Image Work? </p>
</body>
</html>

这是我的 Flask 代码:

app.py

from flask import Flask, render_template
app = Flask(__name__, static_url_path="static", static_folder="static")

@app.route("/")
def main():

    return render_template("index.html")

if __name__ == '__main__':
    app.run()

当我尝试检查页面时收到此错误: 获取http://127.0.0.1:5000/static/images/back.png-404(NOT FOUND)

我浏览了其他几个论坛,但找不到任何解决此问题的方法。我的图片在以下文件夹中:

Flaskapp 模板 静止的 图片 返回.png

当我打开 index.html 文件时,它运行良好,但是当我尝试运行 python 文件来运行服务器时,却看不到背景图像。

不确定如何继续。

【问题讨论】:

  • 您是否尝试将绝对路径放入静态文件夹?
  • 是的,也这样做了。它没有用。我收到一条错误消息,指出不允许加载本地资源。

标签: python css flask background-image


【解决方案1】:

将静态文件夹放在烧瓶应用旁边

/FlaskApp
    -app.py
    static/
        images/
             -back.png
    templates/
         -index.html
         ....

您甚至不必告诉烧瓶...它会知道的

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def main():

    return render_template("index.html")

if __name__ == '__main__':
    app.run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2018-05-08
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多