简体   繁体   English

背景图片404未找到错误python烧瓶

[英]background image 404 not found error python flask

I'm trying to show a background image using python flask and html.我正在尝试使用 python 烧瓶和 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.但我收到错误“加载资源失败:服务器响应状态为 404(未找到)”我认为我的结构很好,但代码如下。

home.py file主页.py 文件

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 index.html 文件

    <!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.我认为这与在 home.py 文件中将图片返回给客户端有关。

Change the URL to this style将 URL 更改为此样式

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/要了解更多信息,请参阅文档: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您必须更改home视图功能并删除最后一行,因为首先此行将永远不会执行,其次根本没有必要,flask 自己分发静态文件,您不应该手动执行
  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:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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