如何解决在python-flask中上传和显示图像
我正在开发一款电子商务应用。在应用程序中,我必须将产品添加到数据库中并显示这些产品。在上传产品时,我还必须上传产品的图片。上传图片后,我必须在 product_detail.html 中显示图片。
这是我上传图片的代码:
UPLOAD_FOLDER = 'static/images'
ALLOWED_EXTENSIONS = set(['jpeg','jpg','png','gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS
#this is to add the product into the product list
@app.route('/AddProduct',methods=["GET","POST"])
def add_product():
if request.method =="POST":
name=request.form['name']
price=request.form['price']
description = request.form['description']
categoryId = int(request.form['category'])
image = request.files['image']
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
print(filename)#img2.png
cursor = mysql.connection.cursor()
cursor.execute('INSERT INTO products VALUES(NULL,% s,% s)',(categoryId,name,price,description,image))
mysql.connection.commit()
return render_template('Shopping.html')
在 product_detail.html 中显示产品 这是我的python代码
@app.route('/product_detail/<int:ProductId>',"POST"])
def product_detail(ProductId):
loggedin = getLoginDetails()
cursor = mysql.connection.cursor()
cursor.execute('SELECT *FROM products WHERE ProductId = % s',(ProductId,))#get the product Id
data=cursor.fetchone()
return render_template('product_detail.html',data=data,loggedin=loggedin)
这是我的 product_detail.html
<div class="images">
<a href="" itemprop="image" class="woocommerce-main-image zoom" title="" data-rel="prettyPhoto">
<img src={{url_for('static',filename='images/'+data[5]) }} id="itemImage"></img></a>
</div>
我可以将图像上传到我的数据库中,并以
的形式存储但我无法在我的 localhost:5000/product_detail/7 中显示图像
图像未显示。我的代码将图像地址作为 "http://localhost:5000/static/images/%3CFileStorage:%20%2717027822406.jpg%27%20%28%27image/jpeg%27%29%3E"
地址必须是“localhost:5000/static/images/17027822406.jpg”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。