skip to Main Content

So, I want an object that will have background image assigned from database. I have found some solutions but none of them had part that would get image from database. This is how I set regular image:

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

So, logically I tried same thing with background>

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

Nope, doesn’t work. Tried different variants without url(‘…’) or something like that.

Anyway if someone has solutions I would apricate it

2

Answers


  1. Try This Once :

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

    or

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

    this can prevent from concatenation error of different data types.
    if you get error after this then try to check what you receive in Location and Location.image_1

    Login or Signup to reply.
  2. I suggest search for errors in browser console. I also suppose that you put images folder in …static/images/

    You also can to use test_request_context to evaluate url_for.
    see: https://flask.palletsprojects.com/en/2.3.x/quickstart/#url-building

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search