如何在flask中设置数据库中的背景图片

问题描述 投票:0回答:1

所以,我想要一个具有从数据库分配的背景图像的对象。我找到了一些解决方案,但它们都没有从数据库获取图像的部分。这就是我设置常规图像的方法:

<img src="{{url_for('static', filename='images/' + Location.image_1)}}" alt="{{Location.name}}">

所以,从逻辑上讲,我尝试了与背景相同的事情>

<div class="" style="background-image: url('{{url_for('static', filename='images/' + Location.image_1)}}') height: 25vh;">

不,不起作用。尝试了不带 url('...') 或类似内容的不同变体。

无论如何,如果有人有解决方案,我会分享它

python html flask flask-sqlalchemy
1个回答
0
投票

尝试一次:

<img src="{{url_for('static', filename=f'images/{Location.image_1}')}}" alt="{{Location.name}}">

<div class="" style="background-image: url('{{url_for('static', filename=f'images/{Location.image_1}')}}') height: 25vh;">

这可以防止不同数据类型的串联错误。 如果在此之后出现错误,请尝试检查您在 Location 和 Location.image_1 中收到的内容

© www.soinside.com 2019 - 2024. All rights reserved.