简体   繁体   中英

background image 404 not found error python flask

I'm trying to show a background image using python flask and html. But I get the error "Failed to load resource: the server responded with a status of 404 (NOT FOUND)" My structre I think is good but heres the code.

home.py file

from flask import render_template, Blueprint, request, send_file
home_page = Blueprint('home', __name__)

@home_page.route('/', methods=['POST', 'GET'])
def home():
    return render_template('index.html')
    return send_file ("home_bg.jpg")

index.html file

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>poo</title>
    <style>
    </style>
</head>
<body style="background-image: url(/static/home_bg.jpg);">

    <script>

    </script>
</body>
</html>

I think it has something to do with returning the picture to the client in the home.py file.

Change the URL to this style

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

To learn more about this here is the documentation: http://flask.pocoo.org/docs/1.0/tutorial/static/

  1. You must change the home view function and remove the last line, because firstly this line will NEVER be executed, and secondly it is simply not necessary, the flask distributes static files itself, you should not do it manually
  2. The line from the template where you upload the image should be changed to the following:
background-image: url( {{ url_for('static', filename='home_bg.jpg') }} )

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM