Search code examples
web2py

How to change the background image using img file from database in Web2py


What I am trying to do is to keep changing the background image using image files that are uploaded in the database instead in the 'static', 'images' folder.

I obviously can't do this in the css, actually, I am not sure if this is even possible. Can I make this work in the layout.html or in the View somehow?

This is how I would display an image in the View if it wasn't intended to be a background-image:

<img src="{{=URL('download', args=pic_row.pics)}}"/>

Of course, this approach to display a background-mage won't work:

<section id="main" class="main row" style="background-image:{{=URL('download', args=pic_row.pics)}}">

..but is there a way I can actually achieve this?


Solution

  • <section id="main" class="main row" style="background-image:{{=URL('download', args=pic_row.pics)}}">
    

    The above method proposed in the question is close, but the CSS syntax is incorrect. The style attribute should be:

    "background-image: url('{{=URL('download', args=pic_row.pics)}}')"