Home > Net >  background image 404 not found error python flask
background image 404 not found error python flask

Time:07-12

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.

CodePudding user response:

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/

CodePudding user response:

  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:

  • Related