Last modified: Jan 10, 2023 By Alexander Williams

I want to use an image from database to use as background image

rev

Saurov

I want to use an image from database to use as background image, different for different data. I used this, but it this doesn't work. What should I do?
My database image folder is media/pics/
Thanks in advance.


How to Create Django Custom 500 and 404 Pages
(04 Comments)
rev

luca

You should use a relative path in css.
Unfortunately django staticfile tag isn't supported.
Example: http://static.mydomain.com/images/logo.png
you should use: background: url('../images/logo.png')
What I've done (because i had the same problem): saving image inside db and then serve it.
style="background-image: url({{ image_field.url }}) Model:
bg_image = models.ImageField(upload_to=bg_path) Full Model and Template: https://codeshare.io/arnOOv

rev

Hassan

well, let's figure out why it's not working.
How? Open the browser dev tools (right-click inspect), put on your frontend developer hat now and forget about Django for a bit, because now you'll be a frontend developer.
Check the your element that has the "top" class, look at its applied style, does it have a background-image? What does the url look like in this world? is it correct? Copy paste that bg url onto your address bar, does the image open up? Is the html element even visible? maybe try putting a "background-color: red" to see it clearly?

rev

Alfonso

Add width and height in style.

rev

ijamakil

If it's local
url("/static/img/restaurant")