我的背景图像仅适用于具有@app.route('/') 的模板。
<header class="intro-header" style="background-image: url('static/img/home.jpg')">
这在以下情况下工作得很好:
@app.route('/')
def home():
return render_template('post.html')
一切正常。我明白了:
127.0.0.1 - - [19/Sep/2016 21:07:11] "GET /static/img/home.jpg HTTP/1.1" 304
但是当我使用相同的模板时:
@app.route('/post/')
def post():
return render_template('post.html')
我明白了:
127.0.0.1 - - [19/Sep/2016 21:15:23] "GET /post/static/img/home.jpg HTTP/1.1" 404 -
背景图像是空白的。
最佳答案
This is a simple problem can solved by Flask documentation
无论如何,你应该在你的模板中使用这样的东西:
background-image: url({{ url_for('static', filename='img/home.jpg') }})
但如果您不想使用 Flask 方法,请使用:
url('/static/img/home.jpg')
或者使用其他网络服务器而不是 flask 默认网络服务器来访问 Apache 等文件,并通过 http://yoursite/static/img/home.jpg
关于python - 如何在 Flask 模板上设置背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39579666/