python 在 html 页面上显示图像

python flask display image on a html page

我正在尝试传递图像的文件名并将其呈现在模板上, 虽然我传递的是实际名称,但它并没有显示在页面上

@app.route('/', methods=['GET','POST'])
@app.route('/start', methods=['GET','POST'])
def start():
person_to_show = 'tim'
profilepic_filename = os.path.join(people_dir, person_to_show, "img.jpg")
return render_template('start.html',profilepic_filename  =profilepic_filename )

例如:profilepic_filename = /data/tim/img.jpg 我试过了

{{profilepic_filename}}
<img src="{{ url_for('data', filename='tim/img.jpg') }}"></img>

我也试过了

<img src="{{profilepic_filename}}"></img>

两者均无效

我在 static 文件夹中创建了 people_photo 并放置了一个名为 shovon.jpg 的图像。从 application.py 我将图像作为变量传递给模板并使用模板中的 img 标签显示它。

application.py中:

from flask import Flask, render_template
import os

PEOPLE_FOLDER = os.path.join('static', 'people_photo')

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = PEOPLE_FOLDER

@app.route('/')
@app.route('/index')
def show_index():
    full_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'shovon.jpg')
    return render_template("index.html", user_image = full_filename)

index.html中:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <img src="{{ user_image }}" alt="User Image">
</body>
</html>

输出: