Home > Software design >  images don't show up when using url_for()
images don't show up when using url_for()

Time:12-26

I am using Tailwind inside my Flask project. And I want to insert a background image using url_for() inside style. I have the usual static folder recommanded by flask and inside it I have an images folder and inside it my background images folder as shown in the code bellow. The problem is that the image don't show up.

<div  style="background-image: {{ url_for('static', filename='images/background/bg1.jpg') }}">

This is the tree of folders

-|App
----|static
----|templates
--------------|Authentication
----------------------------|index.html

I tried url_for() to insert the image.

CodePudding user response:

The issue is the proper formatting of css's background-image attribute. You need to put the link to the image into a special url('') format. In your case, you should be able to reference the image successfully with

<div  style="background-image: url('{{ url_for('static', filename='images/background/bg1.jpg') }}')">
  • Related