Answer a question

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:

@app.route('/')
def home():
    return render_template('post.html')

Everything works. I get this:

127.0.0.1 - - [19/Sep/2016 21:07:11] "GET /static/img/home.jpg HTTP/1.1" 304 

But when I use same template with:

@app.route('/post/')
def post():
     return render_template('post.html')

I get this:

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

And background-image is blank.

Answers

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 :

url('/static/img/home.jpg')

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

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐