Last modified: Jan 10, 2023 By Alexander Williams
I want to use an image from database to use as background image
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.
(04 Comments)
luca
3
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
Hassan
1
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?
Alfonso
0Add width and height in style.
ijamakil
0
If it's local
url("/static/img/restaurant")