Python 使用flask在html页面上呈现来自gridfs的图像

Python 使用flask在html页面上呈现来自gridfs的图像,python,mongodb,image,flask,gridfs,Python,Mongodb,Image,Flask,Gridfs,我想使用flask在html pyge上呈现来自gridfs的图像。 我使用gridfs和一个按钮将映像存储在mongodb中,还使用Flask会话将映像的ObjectId存储在服务器端会话中。 当我单击另一个按钮时,我通过会话获得了之前存储的图像的正确ObjectId,然后希望在html页面中从gridfs呈现此图像,但我不知道如何进行 我的app.py文件: from flask import Flask, render_template, request, redirect, sessio

我想使用flask在html pyge上呈现来自gridfs的图像。 我使用gridfs和一个按钮将映像存储在mongodb中,还使用Flask会话将映像的ObjectId存储在服务器端会话中。 当我单击另一个按钮时,我通过会话获得了之前存储的图像的正确ObjectId,然后希望在html页面中从gridfs呈现此图像,但我不知道如何进行

我的
app.py
文件:

from flask import Flask, render_template, request, redirect, session
from werkzeug.utils import secure_filename
from pymongo import MongoClient
from gridfs import GridFS
from flask_session import Session

DB = 'test-grid'
COLLECT = 'test-session'


client = MongoClient('mongodb://127.0.0.1:27017')
db = client[DB]
fs = GridFS(db)


app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16*1024*1024
app.config['SESSION_TYPE'] = 'mongodb'
app.config['SESSION_MONGODB'] = client
app.config['SESSION_MONGODB_DB'] = DB
app.config['SESSION_MONGODB_COLLECT'] = COLLECT
Session(app)


@app.route("/")
def home():
    return render_template("index.html")


@app.route('/upload', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        filename = secure_filename(f.filename)
        f_id = fs.put(f, filename=filename)
        session['f_id'] = f_id
        session['filename'] = filename
        return redirect('/')


@app.route('/display', methods=['GET'])
def display_file():
    if request.method == 'GET':
        f = fs.get(session['f_id'])
        image = f.read()
        return render_template("index.html", user_image=image)


if __name__ == "__main__":
    app.run(debug=True)

<html lang='en'>
  <head>
    <meta charset='UTF-8'/>
  </head>
  <body>
    <form method="post" action="/upload" enctype="multipart/form-data">
        <input type="file" onchange="this.form.submit()" name="file" autocomplete="off" required>
    </form>
    <form method="get" action="/display">
        <input type="submit" value="Display">
    </form>
    <img src="{{ user_image }}" alt="Show image here"/>
  </body>
</html>


Flask
Flask-Session
pymongo

我的
index.html
文件:

from flask import Flask, render_template, request, redirect, session
from werkzeug.utils import secure_filename
from pymongo import MongoClient
from gridfs import GridFS
from flask_session import Session

DB = 'test-grid'
COLLECT = 'test-session'


client = MongoClient('mongodb://127.0.0.1:27017')
db = client[DB]
fs = GridFS(db)


app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 16*1024*1024
app.config['SESSION_TYPE'] = 'mongodb'
app.config['SESSION_MONGODB'] = client
app.config['SESSION_MONGODB_DB'] = DB
app.config['SESSION_MONGODB_COLLECT'] = COLLECT
Session(app)


@app.route("/")
def home():
    return render_template("index.html")


@app.route('/upload', methods=['POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        filename = secure_filename(f.filename)
        f_id = fs.put(f, filename=filename)
        session['f_id'] = f_id
        session['filename'] = filename
        return redirect('/')


@app.route('/display', methods=['GET'])
def display_file():
    if request.method == 'GET':
        f = fs.get(session['f_id'])
        image = f.read()
        return render_template("index.html", user_image=image)


if __name__ == "__main__":
    app.run(debug=True)

<html lang='en'>
  <head>
    <meta charset='UTF-8'/>
  </head>
  <body>
    <form method="post" action="/upload" enctype="multipart/form-data">
        <input type="file" onchange="this.form.submit()" name="file" autocomplete="off" required>
    </form>
    <form method="get" action="/display">
        <input type="submit" value="Display">
    </form>
    <img src="{{ user_image }}" alt="Show image here"/>
  </body>
</html>


Flask
Flask-Session
pymongo

但这不起作用,我得到以下输出:


有人能帮我修一下吗?也许我可以举出一些例子。

你做错了。您确实不希望在渲染模板中嵌入图像数据。相反,只需将指向API点的路径包含为常规的
img src=“…”
,并创建该API点,该API点只返回图像的二进制文件,就像它是常规文件一样。谷歌对图像“页面优化”的咆哮实际上是指样板内容(即徽标位于页面的右上角,尽管现在我们可能使用字体),而不是每一张图像。浏览器缓存请求。让他们做吧。@NeilLunn你能再详细解释一下你的答案吗。我还是不知道该怎么办,你做错了。您确实不希望在渲染模板中嵌入图像数据。相反,只需将指向API点的路径包含为常规的
img src=“…”
,并创建该API点,该API点只返回图像的二进制文件,就像它是常规文件一样。谷歌对图像“页面优化”的咆哮实际上是指样板内容(即徽标位于页面的右上角,尽管现在我们可能使用字体),而不是每一张图像。浏览器缓存请求。让他们做吧。@NeilLunn你能再详细解释一下你的答案吗。我还是不知道该怎么办。