我正在尝试学习烧瓶
我正在使用烧瓶上传图像
我的代码:-
#main.py
import os
from app import app
import urllib.request
from flask import Flask, flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_image():
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
file_names = []
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_names.append(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
#else:
# flash('Allowed image types are -> png, jpg, jpeg, gif')
# return redirect(request.url)
return render_template('upload.html', filenames=file_names)
@app.route('/display/<filename>')
def display_image(filename):
#print('display_image filename: ' + filename)
return redirect(url_for('static', filename='uploads/' + filename), code=301)
if __name__ == "__main__":
app.run()
upload.html:-
<!doctype html>
<title>Python Flask Upload Multiple Images and Display them</title>
<h2>Select multiple images to upload and display</h2>
<p>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</p>
<form method="post" action="/" enctype="multipart/form-data">
<dl>
<p>
<input type="file" name="files[]" multiple="true" autocomplete="off" required>
</p>
</dl>
<p>
<input type="submit" value="Submit">
</p>
</form>
{% if filenames %}
{% for filename in filenames %}
<div>
<img src="{{ url_for('display_image', filename=filename) }}">
</div>
{% endfor %}
{% endif %}
问题是
当我第一次上传显示的图像时,以及当我上传其他任何图像时,
上一个消失了
我想让它显示“静态/上传”中保存的所有图像