我正在用 Python 和 Flask 開發一個 WEB APP。我想使用 html 影像標簽和按鈕標簽顯示影像以打開瀏覽器并選擇影像并通過使用 Jinja 更新它來顯示在標簽內。我怎么能那樣做?
主要.py:
'''
image_filepath = ''
def browse_img_file():
global image_filepath
path = easygui.fileopenbox(msg="", title="choose an image", multiple=False, filetypes=['.png', '.jpg', '.gif'])
image_filepath = path
@app.route("/")
def index():
global image_filepath
if image_filepath == '':
image_load = url_for('static', filename='/images/no-img.png')
else:
image_load = image_filepath
return render_template("index.html", image=image_load)
@app.route("/upload")
def upload():
global image_filepath
try:
browse_img_file()
except:
pass
finally:
return redirect(url_for("index"))
索引.html:
<div id="CONTENT">
<p>Choose your image.</p>
<div class="img">
<image src="{{ image }}"></image>
</div>
<a href="{{ url_for('upload', image=image) }}">
<button type="button" class="btn btn-primary">Upload an image</button>
</a>
</div>
'''
uj5u.com熱心網友回復:
我認為解決方案是使用<input>而不是在<a>這里:
<div id="CONTENT">
<p>Choose your image.</p>
<div class="img">
<image src="{{ image }}"></image>
</div>
<!-- <a href="{{ url_for('upload', image=image) }}"> -->
<input type="file">
<button type="button" class="btn btn-primary">Upload an image</button>
</a>
</div>
來源:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397302.html
