Yakuzhy Yakuzhy - 3 years ago 331
Python Question

How to set background image on Flask Templates?

My background-image works only for this template that has @app.route('/').

<header class="intro-header" style="background-image: url('static/img/home.jpg')">

This works perfectly fine when:

def home():
return render_template('post.html')

Everything works. I get this: - - [19/Sep/2016 21:07:11] "GET /static/img/home.jpg HTTP/1.1" 304

But when I use same template with:

def post():
return render_template('post.html')

I get this: - - [19/Sep/2016 21:15:23] "GET /post/static/img/home.jpg HTTP/1.1" 404 -

And background-image is blank.

Answer Source

This is a simple problem can solved by Flask documentation

Anyway, you should use something like this in your template:

background-image: url({{ url_for('static', filename='img/home.jpg') }})

but if you don't want to use Flask methods use :


or use another web server instead of flask default web server for your files like Apache and access via http://yoursite/static/img/home.jpg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download