首页 > 解决方案 > 烧瓶静态图像未加载

问题描述

所以我试图在 CSS 中将图像设置为页面的背景,但 Flask 没有正确加载图像。background.jpg位于static/images

background: url("{{ url_for('static', filename='images/background.jpg') }}");
"GET /static/stylesheets/%7B%7B%20url_for('static',%20filename='images/background.jpg')%20%7D%7D HTTP/1.1" 404

我的服务器文件:

from flask import Flask, render_template, url_for

app = Flask(__name__, template_folder = "pages", static_folder="static")


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

if __name__ == "__main__":
    app.run(debug=True, threaded=True, port=80)

标签: pythonflaskjinja2

解决方案


render_template("home.html") 只渲染 html 文件。只有在“<style>”标签中的 html 文件中,css 才会被渲染。如果它的外部文件烧瓶不会对它做任何事情。


推荐阅读